recode as commands.Bot instead of discord.client

This commit is contained in:
2021-11-21 14:19:14 +01:00
parent e9730cbf2f
commit 8864543129
2 changed files with 15 additions and 33 deletions

View File

@@ -1,7 +0,0 @@
def get_help():
help = """
__**Help**__ \n
Prefix: $
hello: get a hello back
"""
return(help)

41
main.py
View File

@@ -15,44 +15,33 @@
#################### ####################
## Imports ## Imports
import discord
import json import json
from discord.ext import commands
from lib.keep_alive import keep_alive from lib.keep_alive import keep_alive
from lib.help import *
## Vars ## Vars
client = discord.Client()
with open("bot.conf", 'r') as f: with open("bot.conf", 'r') as f:
BOTDATA = json.load(f) BOTDATA = json.load(f)
bot = commands.Bot(command_prefix=BOTDATA['prefix'])
## Run ## Run
@client.event ### Events
@bot.event
async def on_ready(): async def on_ready():
print('Logged in as {0.user}'.format(client)) print(f'Logged in as {bot.user}')
for guild in client.guilds: for guild in bot.guilds:
print(f'Connect to Server {guild.name} (id: {guild.id})') print(f'Connect to Server {guild.name} (id: {guild.id})')
@client.event ### Commands
async def on_message(message): @bot.command(name='hello', help='Say hello!')
if message.author.bot: async def say_hello(ctx):
return await ctx.send(f'Hello, <@{ctx.author.id}>!')
print(ctx.guild)
if message.content.startswith(BOTDATA['prefix'] + 'help'): print(ctx.message.content)
help = get_help() print(ctx.message)
await message.channel.send(help) print(ctx.author)
if message.content.startswith(BOTDATA['prefix'] + 'hello'):
await message.channel.send("Hello, {}!".format(message.author))
@client.event
async def on_error(event, *args, **kwargs):
with open('err.log', 'a') as f:
if event == 'on_message':
f.write(f'Unhandled message: {args[0]}\n')
else:
raise
#keep_alive() #keep_alive()
client.run(BOTDATA['token']) bot.run(BOTDATA['token'])