【问题标题】:What can i use instead of 'ctx' in discord.py?我可以在 discord.py 中使用什么来代替“ctx”?
【发布时间】:2021-10-09 03:37:37
【问题描述】:

我有一个名为“静音”的命令:

@commands.command(description="Mutes the specified user.")
@commands.has_permissions(manage_messages=True)
async def mute(self,ctx, member: discord.Member, time = None, *, reason = None):
     guild = ctx.guild   #The error occurs when this line runs
     
     mutedRole = discord.utils.get(guild.roles, name="Muted")
     ......
     ......
     ......

我想在 on_message 事件上使用此方法。我尝试了此方法,但收到一个错误,即“公会”对象没有“公会”属性。因此,我们可以说发生了错误因为 'ctx' 参数的参数不正确。

await self.mute(message.guild,message.author, "1d", reason= "For too many badwords")

那么在 mute 方法中我应该为参数 'ctx' 使用什么参数?

-顺便说一句,当我使用 mute 命令时:'!dc mute @person1 1d for beeeingtoxic' 它工作正常。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    我建议您创建一个单独的函数来静音此人,然后在您的命令和 on_message 触发器上调用它。

    我不知道你的函数看起来如何,但它应该是这样的:

    async def mute_user(self, guild: discord.Guild, member: discord.Member, time = None, reason = None):
        mutedRole = discord.utils.get(guild.roles, name="Muted")
        #the rest of the function
    
    [...]
    
    @commands.command(description="Mutes the specified user.")
    @commands.has_permissions(manage_messages=True)
    async def mute(self,ctx, member: discord.Member, time = None, *, reason = None):
        await self.mute_user(ctx.guild, member, time, reason)
    
    [...]
    
    @commands.Cog.listener()
    async def on_message(self, message):
        [...]
        #i'm just assuming you want to mute the author of the message
        await self.mute_user(message.guild, message.author, "1d", "For too many badwords")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多