discord.py - 我怎样才能制作一个禁止 dm 用户的机器人?
问题描述
import discord
class client(discord.Client):
async def on_ready(self):
print('Logged in!')
async def on_message(self, message):
await ctx.guild.ban(message.author, reason="dm'ed honey le pot")
client = client()
client.run("mytoken")
client.run
我知道ctx.guild.ban
,我不能这样做,因为on_message
没有通过ctx
,但是我如何指定在哪个服务器中禁止?
解决方案
你必须通过它来选择一个公会id
。如果消息是 DM,则没有要引用的公会。
编辑
async def on_message(self, message):
if not message.guild: # Replace 'guild_id' with the guild id you want
self.fetch_guild(guild_id).ban(message.author, reason="dm'ed honey le pot")
推荐阅读
- java - 关于布尔条件(可能是 Java)的双括号的问题?
- javascript - 将用户输入限制为数组中的值
- python - 遍历字符串并打印字符之间的距离
- java - 运行 Maven 插件的单元测试时抛出异常 - ComponentLookupException :: NoSuchElementException :: RepositorySystem
- python - 日期格式为月
- python-3.x - 按数字对图像文件名列表进行排序
- matlab - 在 Matlab 中截断所需支持的泊松分布
- php - 用另一个数组键值 PHP 更改数组键
- json - 将 JSON 字典转换为退出 JSON 文件 Swift
- tensorflow - tf.keras.estimator.model_to_estimator 无法使用自定义和 Lambda 层转换 keras 模型