不在 Discord 中发送欢迎消息(使用 discord.py)

时间:2021-03-17 13:17:04

标签: discord.py python-3.8

我是 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)

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

2 个答案:

答案 0 :(得分:1)

要使 on_member_join 正常工作,您需要在代码和机器人仪表板上启用 Members IntentAPI 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 = ...

答案 1 :(得分:0)

感谢@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}')