我想创建一个黑名单命令来阻止来自特定公会 discord.py 的邀请

如何解决我想创建一个黑名单命令来阻止来自特定公会 discord.py 的邀请

我对这一切都很陌生。我想创建一个命令来阻止用户从列入黑名单的公会发送链接。 该命令将如下所示: !blacklist (guild id) 原因 这是目前的代码

async def server_blacklist(ctx,guild_id: int,*,reason= "no reason provided"):
    guild = client.get_guild(guild_id)
    invitelink = await delete_invite(guild)

我的第一个想法是我需要以某种方式存储公会 ID(在 .txt 或 .db 中)。但我不知道如何。

解决方法

我之前的回答并不令人满意,所以这里有一个新答案:如果您想写入文件,我建议使用 this page

并且更彻底的解释了将某个公会列入黑名单的方法:

我发现可以将某些公会列入黑名单的方法比我之前想象的要简单得多,邀请对象具有公会属性,因此您可以轻松检查。

如何做你想做的事情的“简单”例子是:

@client.command()
async def server_blacklist(ctx,guild_id: int):  # Blacklisting command
    # To add a guild id to the file:
    with open("blacklisted guilds.txt","a") as blacklistfile:  # Open file in append mode
        blacklistfile.write(f"{guild_id}\n")  # Add a new line with the guild id


@client.event
async def on_message(message):  # Event that triggers every time a message is sent
    if "discord.gg" in message.content:  # Check if message has "discord.gg"
        inviteid = message.content.split("discord.gg/")[1].split(" ")[0]  # Get the invite id from the link
        invite = await client.fetch_invite(inviteid)  # Get the invite object from the id
        guild_id = invite.guild.id  # Get the guild id
        # To retrieve the guild ids and check against another
        with open("blacklisted guilds.txt","r") as blacklistfile:  # Open file in reading mode
            for idstring in blacklistfile.read().split("\n"):  # Start iterating through all the lines
                if not idstring == "":  # Check if line has content
                    if int(idstring) == guild_id:  # Check if the id from the file is the same as guild_id
                        await message.delete()
                        await message.channel.send(f"{message.author.mention}! Don't send invites to that server here!")
                        break  # Stop the for loop,since we have already matched the guild id to a blacklisted one

    await client.process_commands(message)  # When using the on_message event and commands,remember to add this,so that the commands still work

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?