简体   繁体   English

如何修复我的 Discord Bot 中的 datetime 命令?

[英]How do I fix my datetime command in my Discord Bot?

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.我最近开始了解 Discord Bots 并尝试自己制作一个,我已经掌握了一些基础知识,但无法获得应该获得当前日期时间的代码的新部分并且想知道我做错了什么。

Edit: This is currently hosted on Heroku so I have no idea how to check for errors编辑:这目前托管在 Heroku 所以我不知道如何检查错误

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:一个更好的选择是使用commands.Bot ,这是一个例子:

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.这个 class 是 discord.Client 的子类,因此你可以用 discord.Client 做任何你可以用这个机器人做的事情。

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.但是,如果您真的想坚持使用discord.Client您可以使用client.dispatch('name of the event', *args)创建自定义事件,遗憾的是没有关于它的文档。

@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 应用程序的日志:

heroku logs -a {name of the app} 
or
heroku logs -a {name of the app} --tail

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复我的 ctx 不工作(Python 中的 Discord Bot)? - How do I fix my ctx not working (Discord Bot in Python)? Python:为什么我的 discord 机器人会为单个命令打印多个输出? 此外,我如何重新启动/关闭我的机器人? - Python: Why does my discord bot print multiple outputs for a single command? Additionally, how do I restart/shut down my bot? 如何让我的 discord 机器人对我的消息做出反应 - How do I get my discord bot to react to my messages 如何让我的 discord python 机器人在命令后识别聊天中的响应? - How do I make my discord python bot recognize response in chat after command? 如何使用 discord 机器人从旧消息/命令中获取内容? - How do I get the content from an old message/command with my discord bot? 我如何制作使我的机器人离开服务器的命令(discord.py) - How do i make a command that make my bot leave from the server (discord.py) 我的 discord 机器人正在对我删除的命令做出反应 - My discord bot is reacting to a command that I deleted 在我的discord机器人键入特定命令后,如何获得所有用户输入的字符串? - How do I get all user input as a string with my discord bot after they type a certain command? 如何做到这一点,以便我的 discord 机器人只接受启动命令的人的输入 - How do I make it so my discord bot only take inputs from the person that started the command (discord.py) 我的机器人的 whois 命令也显示用户的角色,将“@everyone”显示为“@@everyone”我该如何解决这个问题 - (discord.py) My bot's whois command which also displays the user's roles displays “@everyone” as “@@everyone” How do I fix this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM