我想做一个监狱命令,当你说一个!jail(ping)(以分钟为单位的整数)将为ping成员提供一个角色,该角色只允许访问一个通道。 一切都在discord服务器端完成
守则:
import time
from discord.ext import commands
DISCORD_TOKEN = "hahasikeyouwontgetm"
client = commands.Bot(command_prefix="a!")
@client.event
async def on_ready():
guild_count = 0
for guild in client.guilds:
print(f"- {guild.id} (name: {guild.name})")
guild_count = guild_count + 1
print("AnythingBot is in " + str(guild_count) + " guilds.")
@client.command()
async def order(ctx, arg):
if arg == "burger":
embed = discord.Embed(
title="here burger",
color=ctx.author.color
)
embed.set_image(url='https://media.discordapp.net/attachments/839166049755856906/839166099336593428/burger.jpg'
'?width=670&height=670'
)
await ctx.channel.send(embed=embed)
@client.command()
async def math(ctx, arg, arg2, arg3):
mathed = 0
answer = 0
if arg2 == "+":
answer = float(arg) + float(arg3)
mathed = 1
elif arg2 == "-":
answer = float(arg) - float(arg3)
mathed = 1
elif arg2 == "/":
answer = float(arg) / float(arg3)
mathed = 1
elif arg2 == "*":
answer = float(arg) * float(arg3)
mathed = 1
embed = discord.Embed(
title="The answer is: " + str(answer),
color=ctx.author.color
)
if mathed == 1:
await ctx.channel.send(embed=embed)
@client.command()
async def jail(ctx, arg, arg2):
jailtime = int(arg2) * 60
arg = arg.replace("<@!", "")
arg = arg.replace(">", "")
prisoner = await client.fetch_user(user_id=arg)
isadmin = False
admin = discord.utils.get(ctx.guild.roles, name='Admin')
default = discord.utils.get(ctx.guild.roles, name='Member')
jailed = discord.utils.get(ctx.guild.roles, name='Jailed')
embedj = discord.Embed(
title="Jailed " + prisoner.display_name + " for " + str(jailtime) + "m.",
color=ctx.author.color
)
embedn = discord.Embed(
title="You must be a staff member to use this command.",
color=ctx.author.color,
author="loser"
)
embedu = discord.Embed(
title=prisoner.display_name + " has been unjailed.",
color=ctx.author.color
)
if admin in ctx.message.author.roles:
if admin in prisoner:
isadmin = True
await ctx.channel.send(embed=embedj)
await client.add_roles(jailed)
time.sleep(jailtime)
await ctx.channel.send(embed=embedu)
if isadmin:
await client.add_roles(prisoner, admin)
await client.add_roles(prisoner, default)
else:
await ctx.channel.send(embed=embedn)
client.run(DISCORD_TOKEN)
错误:
Traceback (most recent call last):
File "C:\Users\domas\Downloads\DiscordBot\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/domas/Downloads/DiscordBot/main.py", line 87, in jail
if admin in prisoner:
TypeError: argument of type 'User' is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\domas\Downloads\DiscordBot\venv\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\domas\Downloads\DiscordBot\venv\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\domas\Downloads\DiscordBot\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'User' is not iterable
第二个是:
其他一切似乎都起了作用,我感到困惑。 我怎样才能解决这个问题
要修复您的代码,我有一些建议:
commands.MemberConverter
来简化这一过程李>prisoner
无法迭代。您需要获取其.roles
来迭代管理角色李>client.add_roles
向用户添加角色。你需要用discord.Member.add_roles()
来代替李>time.sleep
,而是使用await asyncio.sleep
。异步程序的更好实践。它还可能阻止bot使用其他命令。但请确保导入asyncio李>以下是jail命令的实现(我希望您可以使用这些建议来实现其余命令)
如果
prisoner
只是一个discord.User
对象,则不能使用in
。一份清单就行了相关问题 更多 >
编程相关推荐