【问题标题】:How to make the command can be disabled on certain servers discord py如何在某些服务器上禁用命令 discord py
【发布时间】:2020-11-24 00:24:39
【问题描述】:

我想创建一个列表,其中列出了想要禁用我的机器人的某些功能的 id discord 服务器。 例如:on_member_join 方法在一个人进入服务器时不会发送消息,而在启用此功能的另一端,它会发送该人已连接到服务器。但我不知道如何正确存储 id 并使用它。目前有这样的:

async def serverid(ctx):
  sid = ctx.message.guild.id
  await ctx.send(sid)

sid = 705735563696799723(取决于 ID 服务器)

这大概就是我最终想要得到的结果

async def test(ctx):
 f = open('/app/commands/servers.txt', 'r')
 servers_sid = f.readlines()
 now_sid = ctx.message.guild.id

 if now_sid == servers_sid: #i know servers_sid = ['id'] or something similar this is what i have a problem with
  await ctx.send('Command disabled')
 else:
  #command execution

我知道 servers_sid = ['id'] 或类似的东西,这是我遇到的问题

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您应该使用splitlines,这样您就不会携带\n。我将检查设为not in,如果它不在文件中,那么它将结束

    async def test(ctx):
        with open('/app/commands/servers.txt', 'r') as f:
            servers_sid = f.read().splitlines()
    
        now_sid = str(ctx.message.guild.id)
    
        if now_sid not in servers_sid: 
            await ctx.send('Command disabled')
            return
        
    
    
        await ctx.send('This is working')
        #command execution
    

    我假设你的txt文件是这样的。

    123
    456
    789
    

    【讨论】:

    • 我在两种情况下得到相同的结果(1 - 服务器在列表中,2 - 服务器不在列表中)现在代码:ibb.co/685jbQH ;输出:命令禁用,705735563696799796(now_sid),['705735563696799796'](servers.txt)
    • @THEMakci7m87 请立即检查,我将now_sid 设为字符串
    猜你喜欢
    • 2022-01-12
    • 2021-05-31
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2021-04-11
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多