Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- import time
- import asyncio
- #import os, use to hide the bot token in the the .env file, retrive it and use it here
- #import os (rewrite doesn't allow .env to hide token, or it has changed)
- #serverless sql
- import sqlite3
- #id = 204534035299434497
- #read the token.txt file, get first line with read mode, return first line as it is, save it as token
- def read_token():
- with open("token.txt", "r") as f:
- lines = f.readlines()
- return lines[0].strip()
- token = read_token()
- messages = joined = 0
- client = discord.Client()
- async def update_stats():
- await client.wait_until_ready()
- global messages, joined
- #run while the client isn't closed
- while not client.is_closed():
- try:
- #open the stats.txt file and append (add to the end of) it
- with open("stats.txt" "a") as f:
- f.write(f"Time: {int(time.time())}, Messages: {messages}, Members Joined: {joined}\n")
- messages = 0
- joined = 0
- #delay in seconds
- await asyncio.sleep(3600)
- except Exception as e:
- print(e)
- await asyncio.sleep(3600)
- @client.event
- async def on_ready():
- print('We have logged in as {0.user}'.format(client))
- @client.event
- async def on_message(message):
- #serveridnumber = client.get_guild(204534035299434497)
- #make it so that this will only run in the channel "commands"
- #channels = ["commands"]
- #this limit commands to name of userid in valid users, useful for admin of server and donors
- #valid_users = ["userid"]
- #if str(message.channel) in channels and str(message.author) in valid_users:
- #increase the messages variable by one
- #global messages
- #messages += 1
- #print all message to console
- #print(message.content)
- if message.author == client.user:
- return
- elif message.content.find('!hello') != -1:
- await message.channel.send('Hello!')
- elif message.content == "!users":
- await message.channel.send("""# of Members {serveridnumber.member_count}""")
- elif message.content == "!help":
- embed = discord.Embed(title="Help on BOT", descrption="Some useful commands")
- embed.add_field(name="!hello", value="Greets the user")
- embed.add_field(name="!users", value="Prints number of users")
- await message.channel.send(content=None, embed=embed)
- else:
- print(f"""User:{message.author} tried to do command {message.content}, in channel {message.channel}""")
- @client.event
- async def on_member_join(members):
- #increase the joined variable by one
- #global joined
- #joined += 1
- for channel in member.server.channels:
- if str(channel) == "general":
- await client.send_message(f"""Welcome {member.mention}""")
- @client.event
- async def on_message(message):
- #this will make the update_stats function run as a background loop
- client.loop.create_task(update_stats())
- client.run(token)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement