Discord.py硬币翻转,当硬币为正面(或反面)时会禁止成员。

huangapple go评论67阅读模式
英文:

Discord.py coinflip that will ban a member when it lands on heads (or tails)

问题

I am new to this and I was wondering how to make it work. I spent a lot of time doing it and can't get it right.

This is the code:

@bot.command() 
async def coinflip(ctx):
    if random.choice(determine_flip) == 1:
        embed = discord.Embed(title="Coinflip | (Bot Name)", description=f"{ctx.author.mention} Banned!")
        await ctx.send(embed=embed)
    else:
        embed = discord.Embed(title="Coinflip | (Bot Name)", description=f"{ctx.author.mention} Not Banned!")
        await ctx.send(embed=embed)

I tried to change it like this, still not working (again, I'm sorry, I'm new to this):

async def coinflip(ctx, member): 

    if random.choice(determine_flip) == 1:
        embed = discord.Embed(title="Coinflip | (Bot Name)", description=f"{ctx.author.mention} Banned!")
        await ctx.send(embed=embed)
        await member.ban()
英文:

I am new to this and I was wondering how to make it work. I spent a lot of time doing it and can't get it right.

This is the code

@bot.command() 
async def coinflip(ctx):
    if random.choice(determine_flip) == 1:
        embed = discord.Embed(title="Coinflip | (Bot Name)", description=f"{ctx.author.mention} Banned!")
        await ctx.send(embed=embed)

    else:
        embed = discord.Embed(title="Coinflip | (Bot Name)", description=f"{ctx.author.mention} Not Banned!")
        await ctx.send(embed=embed)

I tried to change it like this still not working (again im sorry im new to this)

async def coinflip(ctx, member): 

if random.choice(determine_flip) == 1:
    embed = discord.Embed(title="Coinflip | (Bot Name)", description=f"{ctx.author.mention} Banned!")
    await ctx.send(embed=embed)
    await member.ban()

答案1

得分: 0

Firstly, determine_flip in your first if-statement is undefined. Secondly, you use await member.ban() in the second example, but you forgot to implement the same logic in the first one.

Try the following:

@bot.command()
async def coinflip(ctx):
    member = ctx.author
    if random.choice([True, False]):
        await ctx.send("%s is being banned!" % member.mention)
        await member.ban()
    else:
        await ctx.send("%s is not being banned!" % member.mention)

The example above bans the user who uses the command if random.choice([True, False]) returns True.

In your second example, you added member as a parameter in the function. Doing this means the command requires an argument to be included when you execute it. However, the argument would only be stored as a string. You'd have to convert it into a discord.Member object.

Below is an example of what I think you may have been trying to do in the second code-block that you provided. Also, I'm setting the member parameter to None by default, so that it doesn't immediately error out if an argument isn't supplied.

@bot.command()
async def coinflip(ctx, *, member: discord.Member = None):
    if member:
        if random.choice([True, False]):
            await ctx.send("%s is being banned!" % member.mention)
            await member.ban()
        else:
            await ctx.send("%s is not being banned!" % member.mention)
    else:
        await ctx.send("You must supply the command with an argument!")

Finally, the example below is a combination of the examples above. In the example below, the target is set to the user who executes the command if an argument is not supplied. Also, it'd be beneficial to note that the bot won't be able to ban users that outrank it in regards to permissions, so implementing a bit of error-handling for such scenarios will be handy in the future.

@bot.command()
async def coinflip(ctx, *, member: discord.Member = None):
    member = member if member else ctx.author
    if random.choice([True, False]):
        try:
            await ctx.send("%s is being banned!" % member.mention)
            await member.ban()
        except discord.errors.Forbidden:
            await ctx.send("Error: %s outranks me!" % member.mention)
    else:
        await ctx.send("%s is not being banned!" % member.mention)

For future reference, it'll be helpful for those who answer your questions if you remember to include the error output within your question.

I hope this helps.

英文:

Firstly, determine_flip in your first if-statement is undefined. Secondly, you use await member.ban() in the second example, but you forgot to implement the same logic in the first one.

Try the following:

@bot.command()
async def coinflip(ctx):
    member = ctx.author
    if random.choice([True, False]):
        await ctx.send("%s is being banned!" % member.mention)
        await member.ban()
    else:
        await ctx.send("%s is not being banned!" % member.mention)

The example above bans the user who uses the command if random.choice([True, False]) returns True.

In your second example, you added member as a parameter in the function. Doing this means the command requires an argument to be included when you execute it. However, the argument would only be stored as a string. You'd have to convert it into a discord.Member object.

Below is an example of what I think you may have been trying to do in the second code-block that you provided. Also, I'm setting the member parameter to None by default, so that it doesn't immediately error out if an argument isn't supplied.

@bot.command()
async def coinflip(ctx, *, member: discord.Member = None):
    if member:
        if random.choice([True, False]):
            await ctx.send("%s is being banned!" % member.mention)
            await member.ban()
        else:
            await ctx.send("%s is not being banned!" % member.mention)
    else:
        await ctx.send("You must supply the command with an argument!")

Finally, the example below is a combination of the examples above. In the example below, the target is set to the user who executes the command if an argument is not supplied. Also, it'd be beneficial to note that the bot won't be able to ban users that outrank it in regards to permissions, so implementing a bit of error-handling for such scenarios will be handy in the future.

@bot.command()
async def coinflip(ctx, *, member: discord.Member = None):
    member = member if member else ctx.author
    if random.choice([True, False]):
        try:
            await ctx.send("%s is being banned!" % member.mention)
            await member.ban()
        except discord.errors.Forbidden:
            await ctx.send("Error: %s outranks me!" % member.mention)
    else:
        await ctx.send("%s is not being banned!" % member.mention)

For future reference, it'll be helpful for those who answer your questions if you remember to include the error output within your question.

I hope this helps.

huangapple
  • 本文由 发表于 2023年6月12日 03:34:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定