python - 公会加入事件打印机器人被邀请 - Discord.py
问题描述
我不确定我将如何编写一个命令/事件,该命令/事件会在将机器人添加到新服务器时打印出机器人所在的行会列表。我知道如何自己打印服务器列表,但我不知道允许我检查机器人何时添加到新服务器的条件。我一直在网上搜索,但没有运气。
@bot.event
async def on_bot_join(bot):
if # condition that sends true when the bot has been added to a new server
print('Bot has been added to a new server')
print('List of servers the bot is in: ')
for guild in bot.guilds:
await print(guild.name)
如果有人能告诉我如何只打印机器人添加到的新服务器的名称,而不是全部打印,那将会更有帮助。
解决方案
是的on_guild_join
,它不需要bot
事件参数。
相反,您传递guild
并on_guild_join
用作您的事件。这里适用于你的问题,
@bot.event
async def on_guild_join(guild):
print('Bot has been added to a new server')
print('List of servers the bot is in: ')
for guild in bot.guilds:
await print(guild.name)
if
添加时也不需要使用
如果你只想打印公会,你可以使用如下并简单地使用guild
@bot.event
async def on_guild_join(guild):
print(f'Bot has been added to: {guild}')
推荐阅读
- ruby-on-rails - 你已经激活了 rack 2.0.3,但是你的 Gemfile 需要 rack 2.0.6
- html - 当一个元素总是包含特定标签时,是否可以选择相邻元素?
- php - 实时数据验证或搜索
- excel - Excel switch 语句公式将“M”或“K”转换为 1000000000 或 1000000
- python - 批量电子邮件发件人的标题“收件人:”
- docker - Docker swarm 不暴露端口
- docker - 如何在终端行中的 Docker 中运行 memSQL 管理命令?
- jquery - 隐藏在中间的粘性菜单出错
- java - 在哪里存储我的 android 包的 PDF 文件
- r - R中大型数据集的ggplot geom_bar