turdbot/py/main.py

109 lines
3.0 KiB
Python
Raw Normal View History

2022-10-25 16:21:21 -06:00
# Copyright (c) 2022, Ian Burgess
# All rights reserved.
#
# This source code is licensed under the GPLv3 license. A copy of this license can be found in the LICENSE file in the root directory of this source tree.
2022-10-10 18:22:20 -06:00
import discord
2022-10-19 10:58:37 -06:00
import random
2022-10-10 18:22:20 -06:00
import datetime
2022-10-25 16:21:21 -06:00
import datetime
import shelve
import backports.zoneinfo as zoneinfo
from py.storage import config
2022-10-25 17:14:44 -06:00
from discord import app_commands
2022-10-10 18:22:20 -06:00
from discord.ext import tasks, commands
2022-10-19 10:58:37 -06:00
2022-10-25 16:21:21 -06:00
quotetime = datetime.time(hour=12, tzinfo=zoneinfo.ZoneInfo("MST"))
2022-10-20 17:56:31 -06:00
avgtimelst = []
2022-10-10 18:22:20 -06:00
2022-10-25 16:21:21 -06:00
token = open("py/TOKEN", "r").read()
2022-10-20 17:56:31 -06:00
intents = discord.Intents.all()
2022-10-25 16:21:21 -06:00
bot = commands.Bot(intents=intents, command_prefix=".")
2022-10-25 17:14:44 -06:00
# on ready
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-25 17:14:44 -06:00
print(bot.tree)
2022-10-19 10:58:37 -06:00
print("discord.py version")
print(discord.__version__)
2022-10-25 17:14:44 -06:00
@bot.hybrid_command(name="eee", with_app_command=True)
2022-10-19 10:58:37 -06:00
async def foo(ctx):
for guild in bot.guilds:
await ctx.send(guild.id)
2022-10-10 18:22:20 -06:00
2022-10-25 16:21:21 -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):
2022-10-25 16:21:21 -06:00
# defining variables
2022-10-10 18:22:20 -06:00
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)
2022-10-25 16:21:21 -06:00
# reply to bots
if message.author.bot and config("replytobot") == "false":
2022-10-10 18:22:20 -06:00
return
2022-10-25 16:21:21 -06:00
# start event timer
2022-10-10 18:22:20 -06:00
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
2022-10-25 16:21:21 -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-25 16:21:21 -06:00
triggers = config("triggerbottriggers", db="data")
while x < len(triggers) and found == "false":
2022-10-10 18:22:20 -06:00
if triggers[x] not in msg:
x = x+1
elif triggers[x] in msg:
found = "true"
2022-10-25 16:21:21 -06:00
replys = config(triggerbotreplys, db=data)
rand = random.randint(0, len(replys))-1
2022-10-10 18:22:20 -06:00
await message.channel.send(replys[rand])
else:
print("something happened")
2022-10-25 16:21:21 -06:00
# daily quote
2022-10-20 17:56:31 -06:00
if channel == config("quotequeue") and config("quotebotenabled") == "true":
2022-10-25 16:21:21 -06:00
file = open("data/quotes.var", "a")
2022-10-10 18:22:20 -06:00
file.write(msg)
file.write(":")
file.close()
2022-10-25 16:21:21 -06:00
# bot commands
2022-10-19 10:58:37 -06:00
await bot.process_commands(message)
2022-10-25 16:21:21 -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)
2022-10-25 16:21:21 -06:00
print("\n end client.event \n microseconds: " +
str(microseconds) + "\n average time: " + str(avgtime))
2022-10-10 18:22:20 -06:00
2022-10-25 16:21:21 -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-25 16:21:21 -06:00
file = open("data/quotes.var", "r+")
2022-10-10 18:22:20 -06:00
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)