【问题标题】:Not sending in Welcome Message in Discord (using discord.py)未在 Discord 中发送欢迎消息(使用 discord.py)
【发布时间】:2021-06-14 20:07:52
【问题描述】:

我是 Python 和 StackOver 流的新手。我正在为我的公会编写一个 Discord Bot。我要添加的第一个功能是在特定频道中发送欢迎消息。我对其进行了编码,运行时没有出现错误……但是当有人加入我的服务器时,机器人没有发送任何消息。

这是我的代码:

from discord.ext import commands
import discord

bot = commands.Bot(command_prefix='-')
TOKEN = '<Bot Token>'

@bot.event
async def on_ready():
print("Succesfully logged in as {0.user}".format(bot))

@bot.event
async def on_memeber_join(member):
try:
    channel = bot.get_channel(<Channel ID>)
    await channel.message.send(
        'Welcome to PRIME CLUB {0.mention}'.format(member))
except Exception as e:
    print(e)


bot.run(TOKEN)

请帮助我纠正我的错误... 提前感谢

【问题讨论】:

    标签: discord.py python-3.8


    【解决方案1】:

    要使on_member_join 工作,您需要在您的代码和机器人的仪表板上启用Members Intent。有关如何执行此操作的信息,请参见 API Docs

    另外,您拼写了member 错误(“memeber”)。您的缩进看起来也有问题(进入函数时有 1 个标签)。

    # Enable intents
    intents = discord.Intents.default()
    intents.members = True
    
    bot = commands.Bot(command_prefix='-', intents=intents)
    
    @bot.event
    async def on_ready():
        # Fix indentation
        print(...)
    
    # Spell the name of the event correctly
    @bot.event
    async def on_member_join(member):
        # Fix indentation
        try:
            channel = ...
    

    【讨论】:

    • 嗯……我必须对范围做任何事情吗……因为它不起作用……我只使用了 Bot 范围
    • 不,您无需执行任何其他操作。你确定你做了我说的一切吗?在代码和仪表板上启用意图并更改函数名称?
    【解决方案2】:

    感谢@stijndcl 的帮助.... 目前这个脚本对我来说效果很好..

    @bot.event
    async def on_member_join(member):
        for channel in member.guild.channels:
            if str(channel.id) == '<CHANNEL ID>':
                await channel.send(f'Welcome!! {member.mention}')
    

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 2021-06-10
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多