【问题标题】:How to ban a discord member that has my bot blocked? discord.py如何禁止我的机器人被阻止的不和谐成员?不和谐.py
【发布时间】:2021-05-13 23:26:25
【问题描述】:

所以我有这个代码,我希望机器人首先将嵌入发送给要被禁止的人,然后我希望机器人禁止他/她,问题是一些用户阻止了我的机器人,因此机器人无法在 DM 中向它们发送消息,从而停止整个命令。如何使用相同的命令禁止他们?

import discord
from discord.ext import commands

class ban(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def ban(self, ctx, member : discord.Member, *, reason = None):
        if (ctx.message.author.permissions_in(ctx.message.channel).ban_members):

            if member.top_role >= ctx.author.top_role:
                    await ctx.send("You can only ban people below you")
                    return
            
            if reason == None:
                reason = "{}: No reason provided.".format(ctx.message.author)
            
            embed=discord.Embed(title="You have been banned!", description="You have been banned from {} for {} by {}".format(ctx.guild, reason, ctx.author), color=0xff0000)
            embed.add_field(name="In order to appeal:", value="Join the server below, then DM me saying '>apelacja'", inline=False)
            embed.set_footer(text="Nims Waifu 2021")

            await member.send(embed=embed)
            await member.send("https://discord.gg/CxTQaYWqRK")
            await member.ban(reason= "{}: {}".format(ctx.author, reason))
            await ctx.send("✅ Banned {} for: {}".format(member, reason))

            

def setup(bot):
    bot.add_cog(ban(bot))

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    你仍然可以禁止公会成员,只是不能发送直接消息。我认为没有直接消息问题的解决方法。由于在member.ban() 之前调用了member.send(),因此将引发异常(Forbidden),并且不会调用member.ban()。所以我建议将member.send() 移动到之后 member.ban() 并创建一个try/except 来处理异常。

    class ban(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
    
        @commands.command()
        async def ban(self, ctx, member : discord.Member, *, reason = None):
            if (ctx.message.author.permissions_in(ctx.message.channel).ban_members):
                ...
                await member.ban(reason= "{}: {}".format(ctx.author, reason))
                try:
                    await member.send(embed=embed)
                    await member.send("https://discord.gg/CxTQaYWqRK")
                except discord.Forbidden:
                    pass   # Or, send a message somewhere notifying of failure
                await ctx.send("✅ Banned {} for: {}".format(member, reason))
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2021-06-06
      • 1970-01-01
      • 2021-07-07
      • 2018-06-27
      • 2020-10-08
      • 2020-06-20
      • 2021-06-20
      • 2021-08-11
      相关资源
      最近更新 更多