使用discord.py创建一个黑色名单的单词列表

发布于 2025-01-27 02:07:42 字数 2247 浏览 3 评论 0原文

我在外部文本文件中创建了一个黑色上列的单词列表,可以使用简单的命令修改。问题在于,我希望所有服务器都能使用我的机器人,并且当服务器修改文件时,对添加机器人的所有服务器进行了修改。我能做些什么?

自动调整事件:

@client.event
async def on_message(message):
    username = message.author.display_name
    msg = message.content
    with open('blacklist.txt', 'r') as f:
        blacklist = f.read()
        if message.author.bot:
            return
        elif msg.lower() in blacklist.split():
            await message.delete()
        else:
            await client.process_commands(message)

我用来在列表中添加更多单词的命令:

@client.command()
async def banword(ctx, arg):
    if ctx.message.author.guild_permissions.administrator:
        text = arg
        file = open('blacklist.txt', 'a+')
        file.write("\n " + text)
        em = discord.Embed(title="banword <arg>",
                           description=f"{ctx.author.mention}, '{text}' has been added to the word's blacklist.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)
    else:
        permission = "administrator"
        em = discord.Embed(title="Permissions Required!",
                           description=f"{ctx.author.mention}, You need the permission '{permission}' to use this command.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)

我用来清除列表的命令:(

@client.command()
async def clearword(ctx):
    if ctx.message.author.guild_permissions.administrator:
        file = open('blacklist.txt', 'w')
        em = discord.Embed(title="clearword",
                           description=f"{ctx.author.mention}, Word's blacklist has been cleared.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)
    else:
        permission = "administrator"
        em = discord.Embed(title="Permissions Required!",
                           description=f"{ctx.author.mention}, You need the permission '{permission}' to use this command.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)

我的代码没有错误,我只需要一个替代方案来实现此目的)

I have created a list of black-listed words in an external text file which can be modified with a simple command. The problem is that I want my bot to be able to be used by all servers, and when a server modifies the file, the modification is affected for all servers that added the bot. What can I do?

The auto-moderation event:

@client.event
async def on_message(message):
    username = message.author.display_name
    msg = message.content
    with open('blacklist.txt', 'r') as f:
        blacklist = f.read()
        if message.author.bot:
            return
        elif msg.lower() in blacklist.split():
            await message.delete()
        else:
            await client.process_commands(message)

The command I use to add more words to the list:

@client.command()
async def banword(ctx, arg):
    if ctx.message.author.guild_permissions.administrator:
        text = arg
        file = open('blacklist.txt', 'a+')
        file.write("\n " + text)
        em = discord.Embed(title="banword <arg>",
                           description=f"{ctx.author.mention}, '{text}' has been added to the word's blacklist.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)
    else:
        permission = "administrator"
        em = discord.Embed(title="Permissions Required!",
                           description=f"{ctx.author.mention}, You need the permission '{permission}' to use this command.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)

The command I use to clear the list:

@client.command()
async def clearword(ctx):
    if ctx.message.author.guild_permissions.administrator:
        file = open('blacklist.txt', 'w')
        em = discord.Embed(title="clearword",
                           description=f"{ctx.author.mention}, Word's blacklist has been cleared.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)
    else:
        permission = "administrator"
        em = discord.Embed(title="Permissions Required!",
                           description=f"{ctx.author.mention}, You need the permission '{permission}' to use this command.",
                           color=discord.Colour.green())
        await ctx.send(embed=em)

(My code has no error, I just need an alternative to achieve this)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗心遗梦遗幸福 2025-02-03 02:07:42

这只是一个不好的解决方案。为此使用数据库更好。您将具有更好的性能等。您应该看一下MongoDB,在数据库中发布值确实很容易,以后从它们中阅读。因此,您的命令类似于!addword,然后将单词发布到数据库中。稍后,您检查单词是否在数据库中,如果是,则将其从Discord聊天中删除,并给出一些黑名单的单词作为响应。

This is just a bad Solution. Using a Database for this is way better. you will have better performance etc. You should take a look at MongoDB it's really easy to post values in the database and read from them later on. so you have a command similar to !addword which then post the word to the database. later you check if the word is in the database and if so, you'll delete it from the discord chat and give some blacklisted word as response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文