discord-bot-base/main.py

97 lines
3.1 KiB
Python

__author__ = "Lukas Mahler"
__version__ = "0.0.0"
__date__ = "06.05.2022"
__email__ = "m@hler.eu"
__status__ = "Development"
# Default
import re
import sys
import time
# Custom
import discord
import requests
from discord.ext import commands
# Self
from src import util, swapgg
class DiscordBot(commands.Bot):
def __init__(self, log, config, swap):
self.member = []
self.log = log
self.log.pipeOut("Bot initializing")
super().__init__(command_prefix=".")
@self.command(name='register')
async def register_to_notifications(ctx, *args):
await self.send_dm(ctx, ctx.message.author, content="test")
@self.command(name='ss',
help="Provides a Screenshot using swap.gg's API (https://docs.swap.gg/#create-screenshot)")
async def doScreen(ctx, *args):
if len(args) < 1:
await ctx.channel.send("Error Missing Inspection Link!")
return
elif len(args) == 1:
# Check if it's a valid inspection Link
inspectionlnk = requests.utils.unquote(args[0])
pattern = re.compile("^steam:\/\/rungame\/730\/\d+\/[+ ]csgo_econ_action_preview ([SM])(\d+)A(\d+)D(\d+)$")
if pattern.search(inspectionlnk):
await ctx.message.add_reaction("")
swap.getScreenshot(inspectionlnk)
while swap.imglnk == "":
time.sleep(0.2)
e = discord.Embed(description=swap.imglnk)
e.set_image(url=swap.imglnk)
await ctx.channel.send(f"{ctx.message.author.mention} -> {swap.imglnk}", embed=e)
else:
await ctx.channel.send(f"Invalid Inspection Link")
else:
await ctx.channel.send(f"The command 'ss' doesn't support multiple arguments")
async def send_dm(self, ctx, member: discord.Member, *, content):
await member.send(content)
self.log.pipeOut(f"DM sent to [{ctx.message.author}] [{content}]")
async def on_ready(self):
self.log.pipeOut("Bot is ready")
# guild = discord.utils.get(self.guilds, name=self.GUILD)
# guilds = [[guild.name, guild.id] for guild in self.guilds]
self.log.pipeOut(f"{self.user} is connected to the following Servers:")
for idx, guild in enumerate(self.guilds):
self.log.pipeOut(f"{idx + 1}. {guild.name} [server_id: {guild.id}]")
async def on_command_error(self, ctx, error):
self.log.pipeOut(f'msg: {ctx} | error: {error}', lvl='ERROR')
def main():
# Start logger
logpath = "./log"
logname = "discord-bot.log"
log = util.Logger(logpath, logname)
# Log any unhandled exception
sys.excepthook = log.unhandledException
# Load toml config
config = util.getConf("prod.toml", log)
# Change loglevel from config
log.changeLevel(config['Log']['level'])
swap = swapgg.Swapgg(log, config)
bot = DiscordBot(log, config, swap)
bot.run(config['Auth']['token'])
if __name__ == '__main__':
main()