首页 > 解决方案 > 我怎样才能制作一个禁止 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,但是我如何指定在哪个服务器中禁止?

标签: discord.py

解决方案


你必须通过它来选择一个公会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")

推荐阅读