软禁令命令(discord.py)

2024-03-29 14:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我目前正在处理一个softban命令,我希望用户获得每个dm发送一个新的inv链接。但它并没有向他们发送信息。这是我的密码。谢谢大家!

@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}")

Tags: nonesendembedawaitctxdiscordgotinv
1条回答
网友
1楼 · 发布于 2024-03-29 14:22:34

代码中存在某种逻辑错误。您必须在try语句中banunban用户。我也会考虑再次查看^ {CD4}},看看你需要什么。

查看以下代码:

@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

可在此处找到相关文档:

相关问题 更多 >