我实际上是在尝试使用discord.py创建一个简单的Discord机器人。当我尝试执行此代码时,问题就出在这里:
import discord
from discord.ext import commands
myid = 3586xxxxxxxxxx
cliente = commands.Bot(command_prefix="!!")
@cliente.command()
async def clear(ctx, amount):
if myid == ctx.author.id:
print("Entering If statement") # This is printed
await ctx.channel.purge(limit=int(amount)) # This is not executed
else:
await ctx.channel.send("You don't have enough permissions.") # This is executed
输出没有意义,当我在服务器上运行“ !! clear 2”时,程序进入If并显示“ Entering the If语句”,它不会删除任何内容,然后bot发送消息在其他地方。
我现在很困惑:s
答案 0 :(得分:0)
我仍然不知道为什么它不起作用,但是,嘿,我发现了一个很好的选择,那就是使用用户名。我举个例子:
import discord
from discord.ext import commands
myuname = "User#1234"
cliente = commands.Bot(command_prefix="!!")
@cliente.command()
async def clear(ctx, amount):
if myuname == str(ctx.author): # For some reason using str() is neccessary or It won't work properly.
print("Entering If statement") # This is printed
await ctx.channel.purge(limit=int(amount)) # This is not executed
else:
await ctx.channel.send("You don't have enough permissions")
就是这样,希望它能帮助到某人:)
答案 1 :(得分:-1)
至于为什么不执行ctx.channel.purge(limit=int(amount))
,我也不知道。
将Else:
替换为Elif:
,这将使该代码块仅在先前的if语句未运行时运行。