我正在制造一个不和谐的机器人,我想输入NSFW命令,所以我使用了一个放置NSFW subreddit随机图像的命令,但是我需要有关NSFW通道检测的帮助,因此该命令不能在非nsfw的通道中使用,并发送一条消息,指出“您需要在nsfw通道中使用此命令!” 这是我的命令,但“其他”部分存在错误:
{{1}}
答案 0 :(得分:0)
您可以添加检查以查看命令所使用的通道是否为NSFW
@commands.command()
@commands.is_nsfw()
async def your_nfsw_command(self, ctx):
#your-command
如果在非nsfw通道中使用命令会引发错误,则可以使用错误处理程序,例如
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.errors.NSFWChannelRequired):
msg.title = "NSFW Command"
msg.description = error.args[0]
return await ctx.send(embed=msg)
或者,您也可以通过执行以下操作添加错误处理程序
commandname.error
,并使用相同的逻辑。
可能的代码更正可能涉及:
if ctx.channel.is_nsfw():
#logic
async with aiohttp.ClientSession() as cs:
#this line seems to be not indented correctly
else:
#logic
没有else
的{{1}}可能会引发您的错误。
答案 1 :(得分:0)
这是我的解决方案,我以前也遇到过同样的问题,但经过研究我找到了解决方案,希望对您有所帮助。
@bot.command()
@commands.is_nsfw()
async def nsfw(ctx):
embed = discord.Embed(title="test", description="test")
async with aiohttp.ClientSession() as cs:
async with cs.get('https://www.reddit.com/r/nsfw/new.json?sort=hot') as r:
res = await r.json()
embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
await ctx.send(embed=embed)
@nsfw.error(ctx, error):
if isinstance(error, NSFWChannelRequired):
await ctx.send(f"Hey! {ctx.author.mention}, sorry but i can't submit nsfw content without nsfw category.")
答案 2 :(得分:-1)
可能你在 else 语句中缺少括号
<块引用>代码
async def nsfw(ctx):
if ctx.channel.is_nsfw():
embed = discord.Embed(title="test", description="test")
async with aiohttp.ClientSession() as cs:
async with cs.get('https://www.reddit.com/r/nsfw/new.json?sort=hot') as r:
res = await r.json()
embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
await ctx.send(embed=embed)
else:
await ctx.send("You need to use this command in a nsfw channel!")