I've recently began to learn about Discord Bots and tried to make one myself, I've got some basics working but haven't been able to get a new section of the code working which is supposed to get the current datetime and was wondering what I've done wrong.
Edit: This is currently hosted on Heroku so I have no idea how to check for errors
import discord
from datetime import datetime
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
@client.event
async def on_member_join(member):
print(f'{member.name} has joined the server')
channel = client.guilds[0].get_channel(745404752161931266)
print(channel)
await channel.send(f'{member.name} has joined the server')
@client.event
async def on_member_remove(member):
print(f'{member.name} has left the server')
channel = client.guilds[0].get_channel(745404752161931266)
print(channel)
await channel.send(f'{member.name} has left the server')
@client.event
async def on_message(message):
channel = client.guilds[0].get_channel(765757497155649567)
if message.content.find("!hello") != -1:
await message.channel.send("Hello!")
@client.event
async def on_message(message):
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
if message.content.find("!datetime") != -1:
await message.channel.send("date and time =", dt_string)
client.run('[my_token]')
You can't have more than one the same listener, you have to put your whole code in one
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
# ...
elif message.content.startswith('!datetime'):
# ...
A better alternative to that is using commands.Bot
, here's an example:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
@bot.command()
async def hello(ctx):
await ctx.send('Hello!')
# To invoke: `!hello`
bot.run('token')
This class is a subclass of discord.Client and as a result anything that you can do with a discord.Client you can do with this bot.
But if you really wanna stick with discord.Client
you can create custom events with client.dispatch('name of the event', *args)
sadly there's no docs about it.
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
# Dispatching the custom event
client.dispatch('hello', message)
@client.event
async def on_hello(message):
"""Notice how the event it's called `on_hello` not `hello` and
takes the same arguments that we specified in `client.dispatch`"""
await message.channel.send('Hello!')
Also to check the logs from your heroku app:
heroku logs -a {name of the app}
or
heroku logs -a {name of the app} --tail
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.