turdbot/bot.py

131 lines
3.3 KiB
Python
Raw Normal View History

2022-10-19 10:58:37 -06:00
#imports
2022-10-10 18:22:20 -06:00
import discord
2022-10-20 17:56:31 -06:00
import shelve
2022-10-19 10:58:37 -06:00
import random
import configparser
2022-10-18 15:03:06 -06:00
import backports.zoneinfo as zoneinfo
2022-10-10 18:22:20 -06:00
import datetime
from discord.ext import tasks, commands
2022-10-19 10:58:37 -06:00
2022-10-18 15:03:06 -06:00
quotetime = datetime.time(hour=12,tzinfo=zoneinfo.ZoneInfo("MST"))
2022-10-20 17:56:31 -06:00
server = "global"
avgtimelst = []
2022-10-10 18:22:20 -06:00
2022-10-20 17:56:31 -06:00
token = open("TOKEN","r").read()
intents = discord.Intents.all()
bot = commands.Bot(intents=intents,command_prefix=".")
2022-10-10 18:22:20 -06:00
2022-10-20 17:56:31 -06:00
#loading config files
def config(value,db="config",serverid=server,mode="r"):
global = {
"replytobot":"false",
"triggerbotenabled":"true",
"quotebotenabled":"true",
"triggerbottriggers":["hello","hi","howdy"],
"quotequeue":"1010042640508669982",
}
data = shelve.open("bot.shlf",writeback=True)
if mode == "r":
try:
return data[db][serverid][value]
except:
return global[value]
elif mode == "w":
data[db][serverid] = value
return("success")
else:
print("error")
data.close()
2022-10-10 18:22:20 -06:00
#bot info
2022-10-19 10:58:37 -06:00
@bot.event
2022-10-10 18:22:20 -06:00
async def on_ready():
2022-10-19 10:58:37 -06:00
print('We have logged in as {0.user}'.format(bot))
2022-10-10 18:22:20 -06:00
if not dailyquote.is_running():
dailyquote.start()
2022-10-19 10:58:37 -06:00
print("discord.py version")
print(discord.__version__)
@bot.command()
async def foo(ctx):
for guild in bot.guilds:
await ctx.send(guild.id)
2022-10-10 18:22:20 -06:00
#main event
2022-10-19 10:58:37 -06:00
@bot.event
2022-10-10 18:22:20 -06:00
async def on_message(message):
#defining variables
msg = str(message.content)
channel = str(message.channel.id)
2022-10-20 17:56:31 -06:00
server = message.guild.id
2022-10-10 18:22:20 -06:00
author = str(message.author)
#reply to bots
2022-10-20 17:56:31 -06:00
if message.author.bot and config("replytobot") == "false":
2022-10-10 18:22:20 -06:00
return
#start event timer
print("\n start client.event")
2022-10-20 17:56:31 -06:00
start = datetime.datetime.now()
2022-10-10 18:22:20 -06:00
#triggerbot
2022-10-20 17:56:31 -06:00
if config("triggerbotenabled") == "true":
2022-10-10 18:22:20 -06:00
x = 0
found = "false"
2022-10-20 17:56:31 -06:00
triggers = config("triggerbottriggers",db="data")
2022-10-10 18:22:20 -06:00
while x < len(triggers) and found == "false":
if triggers[x] not in msg:
x = x+1
elif triggers[x] in msg:
found = "true"
2022-10-20 17:56:31 -06:00
replys = config(triggerbotreplys,db=data)
2022-10-10 18:22:20 -06:00
rand = random.randint(0,len(replys))-1
await message.channel.send(replys[rand])
else:
print("something happened")
#daily quote
2022-10-20 17:56:31 -06:00
if channel == config("quotequeue") and config("quotebotenabled") == "true":
2022-10-10 18:22:20 -06:00
file = open("data/quotes.var","a")
file.write(msg)
file.write(":")
file.close()
2022-10-19 10:58:37 -06:00
#bot commands
await bot.process_commands(message)
2022-10-10 18:22:20 -06:00
#end function timer
2022-10-20 17:56:31 -06:00
end = datetime.datetime.now()
eventime = (end - start)
microseconds = eventime.microseconds
avgtimelst.append(microseconds)
avgtime = sum(avgtimelst) / len(avgtimelst)
print("\n end client.event \n microseconds: " + str(microseconds) + "\n average time: " + str(avgtime))
2022-10-10 18:22:20 -06:00
2022-10-16 13:24:16 -06:00
@tasks.loop(time=quotetime,reconnect=True)
2022-10-10 18:22:20 -06:00
async def dailyquote():
2022-10-20 17:56:31 -06:00
if config("quotebotenabled") == "true":
2022-10-10 18:22:20 -06:00
file = open("data/quotes.var","r+")
list = file.read().split(":")
rand = random.randint(0, (len(list)-1))
print(rand)
2022-10-19 10:58:37 -06:00
channel = bot.get_channel(int(quotebotconf["config"]["dailyquote"]))
2022-10-16 13:24:16 -06:00
await channel.send(list.pop(rand))
file.write(":".join(list))
file.close()
2022-10-10 18:22:20 -06:00
2022-10-20 17:56:31 -06:00
bot.run(token)