首页 > 解决方案 > Softban command(discord.py)

问题描述

So im currently working on an softban command and i want that the user gets send a new inv link per dm. But it isnt sending them a message. Thats my code. Thank you!

@client.command(pass_context=True)
async def softban(ctx, user: discord.User=None):
    if user == None:
        embed = discord.Embed(title=f"**Softban**",
                              description=f"Softban will ban but immediately unban the user and send them a new invite link!"
                                          f"\nUsage: {prefix}softban <User>")
        await ctx.send(embed=embed)
    try:
        inv = await ctx.author.guild.create_invite(max_uses=1)
        await user.send(f"You got softbanned from {ctx.author.guild}\nJoin again with this link: {inv}")
    except:
        await ctx.send("User probably has their dms closed!")
    await ctx.guild.ban(user)
    await asyncio.sleep(0.1)
    await ctx.guild.unban(user)
    await ctx.send(f"{user.mention} got softbanned!")
    print(f"{user} got softbanned from {ctx.guild.name}")

标签: pythondiscord.py

解决方案


您的代码中有某种逻辑错误。你必须banunban用户在你的try声明中。我也会考虑exceptions再看一遍,看看你需要什么。

看看下面的代码:

@client.command(pass_context=True)
async def softban(ctx, user: discord.Member = None):
    if user == None:
        embed = discord.Embed(title=f"**Softban**",
                              description=f"Softban will ban but immediately unban the user and send them a new invite link!"
                                          f"\nUsage: softban <User>")
        await ctx.send(embed=embed)
    try:
        inv = await ctx.channel.create_invite(max_uses=1)
        await user.send(f"You got softbanned from {ctx.author.guild}\nJoin again with this link: {inv}")
        await ctx.guild.ban(user)
        await asyncio.sleep(0.1)
        await ctx.guild.unban(user)
        await ctx.send(f"{user.mention} got softbanned!")
        print(f"{user} got softbanned from {ctx.guild.name}")
    except:
        # Do whatever you want to if DMs are closed (Ban/Unban)
  • 我们只能创建一个文本频道的邀请,所以我们使用ctx.channel.create_invite
  • 因为user我们说它必须是discord.Member

可以在此处找到相关文档:


推荐阅读