【问题标题】:How to make a NSFW command in discord.py?如何在 discord.py 中创建 NSFW 命令?
【发布时间】:2020-12-12 01:03:13
【问题描述】:

我正在制作一个不和谐的机器人,我想要一个 NSFW 命令,所以我使用了一个放置 NSFW subreddit 的随机图像的命令,但我需要帮助检测 NSFW 频道,所以这个命令不能在非 nsfw 的频道中使用,并发送一条消息说“您需要在 nsfw 频道中使用此命令!” 这是我的命令,但是“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!"

【问题讨论】:

  • 你遇到了什么错误?

标签: python discord bots discord.py


【解决方案1】:

您可以添加检查以查看正在使用该命令的频道是否为 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

您的错误可能是由没有ifelse 引发的。

【讨论】:

    【解决方案2】:

    可能你在 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!")
    

    【讨论】:

      【解决方案3】:

      这是我的解决方案,我以前也遇到过同样的问题,但研究我找到了解决方案,希望对您有所帮助。

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

      【讨论】:

        猜你喜欢
        • 2021-03-19
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 2023-02-26
        • 2021-06-20
        • 2021-01-16
        • 1970-01-01
        • 2021-05-11
        相关资源
        最近更新 更多