在Discord.py中将成员引用为变量 [英] Referring to a member as a variable in Discord.py

查看:134
本文介绍了在Discord.py中将成员引用为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个运行聊天过滤器的不和谐机器人;我的最终目标是使机器人具有单词列表,如果说出列表中的单词中的一个,则该机器人将向成员添加提示符,如果它们得到3,则将运行从服务器中踢出的命令.

I'm trying to make a discord bot that runs chat filter; my end goal is for a bot to have a list of words and if one of those words from the list is said, the bot will add a tally to a member and if they get like 3 then a command to kick from server will run.

创建列表很容易,我知道如何编写命令来踢某人,但是我绝对不知道如何让该机器人跟踪每个成员的值....我需要设置该成员的ID吗?作为变量?

Creating the list is easy and I know how to write the command to kick someone, but I am absolutely lost on how I would get the bot to track values for each member.... Would I need to set the member's ID as a variable?

我们非常感谢您的帮助,我完全不为所动,并且对discord.py模块的经验有限.

Any help is appreciated, I am just absolutely stuck and have limited experience in the discord.py module.

import asyncio
import json
import discord
from discord.ext import commands
from discord.ext.commands import Bot

bot = commands.Bot(command_prefix='#')

infractions = {}
blacklist = ["bad", "word"]
limit = 5


@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')


@bot.event
async def on_ready():
    global infractions
    try:
        with open('infractions.json') as f:
            infractions = json.load(f)
    except FileNotFoundError:
        print("Could not load infractions.json")
        infractions = {}


@bot.event
async def on_message(message):
    content = message.content.lower()
    if any(word in content for word in blacklist):
        id = message.author.id
        infractions[id] = infractions.get(id, 0) + 1
        if infractions[id] >= limit:
            await bot.kick(message.author)
        else:
            warning = f"{message.author.mention} this is your {infractions[id]} warning"
            await bot.send_message(message.channel, warning)
    await bot.process_commands(message)


@bot.command()
async def save():
    with open('infractions.json', 'w+') as f:
        json.dump(infractions, f)

推荐答案

您可以保留一个包含违规数量的成员ID字典.您可以从message.author.id属性

You can keep a dictionary of member ids to number of infractions. You can get the id from the message.author.id attribute

from discord.ext import commands

infractions = {}
blacklist = ["bad", "word"]
limit = 5

bot = commands.Bot('!')    

@bot.event
async def on_message(message):
    content = message.content.lower()
    if any(word in content for word in blacklist):
        id = message.author.id
        infractions[id] = infractions.get(id, 0) + 1
        if infractions[id] >= limit:
            await bot.kick(message.author)
        else:
            warning = f"{message.author.mention} this is your {infractions[id]} warning"
            await bot.send_message(message.channel, warning)
    await bot.process_commands(message)

我建议使用ids作为键而不是Member对象,以便您可以更轻松地将文件另存为JSON,这将允许您在关闭漫游器时保留值. 查看此我写的另一个答案是如何执行此操作的示例

I suggest using ids as keys rather than Member objects so that you can more easily save the file as a JSON, which will allow you to persist values when you shut off your bot. See this other answer I wrote for an example of how to do that

这篇关于在Discord.py中将成员引用为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆