1
#discordbot.py

import discord
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
    print('Bot is ready.')

@client.event
async def on_member_join(member):
    print(f'{member} has joined a server.')

@client.event
async def on_member_remove(member):
    print(f'{member} has left a server.')

我用这个脚本运行了python。但是 on_member_join(member): ~ 和 on_member_remove(member): ~ 没有一些变化。为什么不知道为什么,我是 python 新手。我希望你能帮助我。

4

2 回答 2

0

Print 只会在控制台中打印出字符串。我假设您希望消息出现在指定的频道中,因此您可以执行以下操作:

import discord
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
    print('Bot is ready.')

@client.event
async def on_member_join(member):
    channel = client.get_channel('insert the channel id of your choice')
    await channel.send(f'{member} has joined a server.')

@client.event
async def on_member_remove(member):
    channel = client.get_channel('insert the channel id of your choice')
    await channel.send(f'{member} has left a server.')
于 2021-03-04T00:30:34.263 回答
0

我看到了你的代码,我可以告诉你我遇到了同样的问题。您只需在您的开发者页面 ( https://discord.com/developers/applications/YOUR_BOT_ID/bot ) 中启用“服务器成员意图”。然后进入你的机器人并添加一些新行:

intents = discord.Intents.default()
intents.members = True

然后,进入客户端 var 定义:

client = commands.Bot(command_prefix = '.', intents=intents)

你准备好了!我希望我对你有帮助!

于 2021-03-08T17:24:53.003 回答