简体   繁体   中英

Discord.py bot gives a block when ban a user. How can I fix that?

When I ban a user using the command, it blocks him/her, so when I unban a user, I want to send to him/her an embed telling him/her "you banned has been removed" + "server link", but it can't be sent to him because the bot already blocked the user. How can I fix that?

banned command

from discord.ext import commands
from discord import Intents
import datetime
import discord
import time

# Ban command
@bot.command()
@commands.has_permissions(ban_members=True)
async def ban(ctx: discord.member, member: discord.Member, *, reason=None):
    guild = bot.get_guild(920733512124989461) # Get server id to make action
    channel = guild.get_channel(974470211891773440) # log channel id
    # get current time
    now = datetime.now()
    current_time = now.strftime("%H:%M")
    # dm banned embed
    emDm = discord.Embed(title="Moderation System", description="**You were banned from the 
    server**", color=0xe54b4b)
    #===================================
    # log banned embed
    emBan = discord.Embed(title=None, description=None, color=0xe54b4b)
    emBan.add_field(name="Ban action", value=f"**{member.mention} has been banned by 
    {ctx.author.mention}**", inline=False)
    #===================================
    # if the reason is empty write no reason
    if reason == None:
        reason="no reason"
        emDm.add_field(name="Reason", value=f"{reason}")
        await member.send(embed=emDm) # sent DM embed
        await ctx.guild.ban(member)
        emBan.add_field(name="Reason", value=f"{reason}", inline=False)
        await ctx.send(f"> **{member} has been banned by {ctx.author}**") # sent to user
        await channel.send(embed=emBan) # sent to log # sent to log
        print(f"{current_time}: {member} has been banned by {ctx.author} for {reason}")
    else:
        emDm.add_field(name="Reason", value=f"{reason}")
        await member.send(embed=emDm)
        await ctx.guild.ban(member)
        emBan.add_field(name="Reason", value=f"{reason}", inline=False)
        await channel.send(embed=emBan)
        print(f"{current_time}: {member} has been banned by {ctx.author} for {reason}")
# Ban error
@ban.error
async def ban_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("> **You can't make this action!**")
        now = datetime.now()
        current_time = now.strftime("%H:%M")
        print(f"{current_time}: {ctx.author} tried to ban\nUser id : {ctx.author.id}\n")
        time.sleep(1)
             await ctx.channel.purge(limit=1)

Unban command

# unBan command
@bot.command()
@commands.has_permissions(ban_members=True)
async def unban(ctx : discord.Member, id: int):
    user = await bot.fetch_user(id)
    #=================================
    guild = bot.get_guild(920733512124989461) # Get server id to make action
    channel = guild.get_channel(974470211891773440) # sent to log channel
    now = datetime.now()
    current_time = now.strftime("%H:%M")
    #==================================
    # Private message embed
    emDm = discord.Embed(title="Moderation System", description=f"**Your banned has been 
    removed by {ctx.author}**", color=0xe54b4b)
    #===================================
    # log unbanned embed
    emUnban = discord.Embed(title="Moderation System", description=f"{user} has been 
    pardoned", color=0xFFB433)
    try:
        await ctx.guild.unban(user)
        await ctx.reply(f"> **Ban has been removed from {user}**", mention_author=False)
        await channel.send(embed=emUnban) # sent to log channel
        await user.send(emDm) # Sent dm embed
        print(f"{current_time}: {user} has been unban by {ctx.author}\n ")
    except discord.Forbidden:
        await ctx.send(f"I don't have permission to make this action")
    # unBan error
    @unban.error
    async def unban_error(ctx, error):
        if isinstance(error, commands.MissingPermissions):
            await ctx.send("> **You can't make this action**")
            now = datetime.now()
            current_time = now.strftime("%H:%M")
            print(f"{current_time}: {ctx.author} tried to unban\nUser id : {ctx.author.id}\n")
            time.sleep(1)
            await ctx.channel.purge(limit=1)

Thanks for helping me :D

遗憾的是,如果用户没有更改默认设置,如果他们不一起共享服务器,您将无法向该用户发送消息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM