【问题标题】:how to make a command only work in a specific channel using discord.py如何使用 discord.py 使命令仅在特定通道中工作
【发布时间】:2021-07-02 04:17:13
【问题描述】:

目前我正在使用

vouch_channel = client.get_channel(828696942250295316)

@client.command()
async def vouch(message):
  async def vouch(ctx,member : discord.Member):

    if ctx.author == member:
        await ctx.send("You can't vouch yourself")

       
      if message.channel.id == vouch_channel.id:


        auth = ctx.author and ctx.author.mention and ctx.author.id
        await open_vouches(ctx.author)
        await open_vouches(member)



        await update_vouches(member,+1,'vouches_gotten')
        await update_vouches(ctx.author,+1,'vouches_given')      
        await ctx.reply(f'<:icon_checkmark:827639277285408829> {ctx.author.mention} You vouched for {member}!')
       print('[LOGS] bot was used for vouch')



      else:
            #command attempted in non command channel - redirect user
            await message.channel.send('Write this command in {}'.format(vouch_channel.mention))

尝试这样做,我尝试改变周围的东西,但没有奏效。

我真的不知道该怎么办,请帮我一下吧

我也三合会做

@client.command()
async def vouch(message,ctx,member : discord.member):

我没有做我在那里做过的事情,我也尝试过

@client.command()
async def vouch(message)(ctx,member : discord.member):

但这也没有用。

请帮帮我

【问题讨论】:

    标签: python json discord discord.py


    【解决方案1】:

    乍一看,您的函数中的参数顺序似乎是关闭的。如果您不使用 cog,则 ctx 应该是要传递的第一个参数。 您可能还会遇到多字消息的问题,因此您可以使用 * 参数表示“传递的第二个参数之后的所有内容都是消息的一部分”。

    试试下面的代码。您可以按如下方式使用该命令:!vouch @Kelo This is my message

    @client.command()
    async def vouch(ctx, member : discord.Member, *, message):
      if ctx.author == member:
        await ctx.send("You can't vouch yourself")
        return
      elif ctx.channel.id != 828696942250295316:
        await ctx.send("You can't vouch in this channel")
        return
      
      #Rest of your code
    

    【讨论】:

    • ! 的前缀吗?还要添加其余代码我会做```其他:```
    • 我要怎么做才能有 2 个频道?
    • 是的,它是前缀。并且你可以在 if 条件中使用 and 运算符。
    • 嘿,你能帮我解决我在stackoverflow.com/q/66970008/15564722 的另一个问题吗,因为我真的不知道该怎么做,因为这只适用于这个命令而不是那个命令
    • 如果您想拥有 2 个频道,您可以轻松制作支持担保的频道列表。然后,您检查,如果 ctx.channel.id 不在列表中,则不能保证;否则你就被担保了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2021-11-18
    • 2021-09-01
    • 1970-01-01
    • 2020-12-03
    • 2020-10-25
    • 2021-08-17
    相关资源
    最近更新 更多