【问题标题】:Discord.py modmail systemDiscord.py modmail系统
【发布时间】:2021-04-28 20:26:11
【问题描述】:

基本上我想制作一个 modmail 系统,一旦有人 dms 机器人,它就会打开一个频道,让 DMed 机器人的人可以通过机器人进行对话。模组会通过频道进行交流。

问题是,我知道如何做大部分事情,但我被困在其中一件事情上。这是我的代码:

@client.event()
async def on_message(message):
    if str(message.channel.type) == "private":
        guild = client.get_guild()
        await guild.create_text_channel(ctx.author.name)
    else:
        return

这就是我的代码,它基本上创建了一个以用户名命名的频道。现在我陷入困境的是:如何让它将消息发送到该特定频道,创建后如何获取频道的 ID 或名称?

【问题讨论】:

  • 你是如何创建频道的?我没有看到任何触发机器人的事件。 if str(message.channel.type) == "private" 在做什么?它是如何工作的?
  • @Dominik 事件是 on_message,if 部分告诉机器人只考虑 DM 而不是服务器消息
  • 如果您有其他方法可以只考虑您认为比我更好的 DM,请告诉我,即使我是高级 discord.py 开发人员,我仍然需要学习一些东西!

标签: python-3.x discord discord.py


【解决方案1】:

我在您的代码中看到一些错误,这些错误通常会阻止事件或无法按您希望的方式工作。

第一:client.event不是client.event()。括号仅用于命令。

其次:您不能像使用message 事件一样使用ctx.author.name。要获取作者,您必须使用message.author.name

第三:您必须定义await 函数才能向所需频道发送消息。

查看完整代码:

@client.event
async def on_message(message):
    if str(message.channel.type) == "private":
        guild = client.get_guild(YourGuildID)
        modmail = await guild.create_text_channel(message.author.name)
        await modmail.send("This is a test") # Send whatever you want to
    else:
        return

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2021-06-12
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多