Files
bot-discord/main.py
2021-11-30 23:20:05 +01:00

79 lines
1.9 KiB
Python

####################
# == Main Info ==
# main.py
# by 4nima
# v.0.0.0
#
####################
# == Descrtiption ==
# template for discord bot
#
####################
# == Dependencies ==
# pip3 install discord
# pip3 install flask (for keep_alive)
####################
## Imports
import asyncio as aio
import logging as log
import json
import random
from discord.ext import commands
#from lib.keep_alive import keep_alive
## Vars
### Load and set Bot Config
with open("bot.conf", 'r') as f:
BOTDATA = json.load(f)
bot = commands.Bot(command_prefix=BOTDATA['prefix'])
### Set logging options
log.basicConfig(
filename="bot.log",
level=log.DEBUG,
format='%(asctime)s - %(process)d-%(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
## Run
### Events
@bot.event
async def on_ready():
log.info(f'Logged in as {bot.user}')
print(f'Logged in as {bot.user}')
for guild in bot.guilds:
log.info(f'Connect to Server {guild.name} (id: {guild.id})')
@bot.event
async def on_reaction_add(reac, usr):
print(reac)
print(usr)
### Commands
@bot.command(name='hello', help='Say hello!')
async def say_hello(ctx):
async with ctx.typing():
log.info('Start say_hello function')
await ctx.message.add_reaction('🤔')
await ctx.send(f'Hello, <@{ctx.author.id}>!')
print(ctx.guild)
print(ctx.message.content)
print(ctx.message)
print(ctx.author)
await aio.sleep(random.randint(1,5))
await ctx.message.remove_reaction('🤔', bot.user)
if random.randint(1,100) < 10:
log.error('say_hello function was failed')
await ctx.message.add_reaction('')
else:
log.info('Run say_hello successfully')
await ctx.message.add_reaction('')
await aio.sleep(10)
await ctx.message.delete()
#keep_alive()
bot.run(BOTDATA['token'])