通过 discord bot 为每个人设置唯一的关键字
Setting unique keywords for each person via discord bot
我目前正在尝试制作一个允许成员设置特定关键字的机器人,然后机器人会在服务器中检查(在一段时间内)。如果机器人在消息中检测到该关键字(仅通过另一个 bot/webhook),它会提醒设置该关键字的用户。
基本上我想做的是这样的场景:
成员 1 --->
!setkeyword new link
(bot 然后将成员 1 的关键字具体设置为 "new link")
!listkeywords
(bot returns word/phrase "new link")
成员 2 --->
!setkeyword 新鞋
(bot 然后将成员 1 的关键字具体设置为 "new shoe")
!listkeywords
(bot returns word/phrase "new shoe")
我最擅长的是使用字典和列表。字典的关键是设置关键字的成员的用户标识,列表包含关键字。
变体 1:
字典 = {}
@bot.command()
async def add(ctx,keyword):
listy = []
listy.append(keyword)
dictt[ctx.author.id] = listy
变体 2:
dictt = {}
listy = []
@bot.command()
async def add(ctx,keyword):
listy.append(keyword)
dictt[ctx.author.id] = listy
变体 1:这导致每次用户向机器人发送消息时都会生成一个新列表。这意味着对于每个唯一用户,列表中只有一个关键字。因此,如果用户尝试添加多个关键字,则只会将最新的关键字添加到列表中
变体 2:这导致来自唯一用户的关键字被添加到同一个列表,这意味着每个用户可以添加多个关键字,但它们对每个用户来说并不是唯一的。
如何让每个唯一用户拥有自己唯一的列表,并且仍然能够向其中添加多个关键字?
有一个全局 dict
并在追加之前检查用户的 ID 是否存在,如果不存在则用新词创建一个新列表。
memory = {}
def add(id, word):
if id in memory.keys():
memory[id].append(word)
else:
memory[id] = [word]
我的建议是保存文件。设置变量将在关闭或重新启动或崩溃时被破坏...
filepath = os.path.dirname(os.path.realpath(__file__))
config = configparser.ConfigParser()
config.optionxform = str
try:
config.read(f'{filepath}/data/keywords.cfg')
user1 = config['User1'] #This might be an Idea if you have every user stored in vars
except Exception as error:
print(f" -- ERROR : File not detected.\n{error}")
quit()
#Or use it like this
@bot.command()
async def add(ctx,keyword):
keywordlist = config.items(ctx.message.user.id, raw=True)
keywordlist.append(keyword)
config[ctx.message.user.id] = dict(keywordlist)
@bot.command()
async def list(ctx):
keywordlist = config[ctx.message.user.id]
await ctx.send(keywordlist)
文件看起来像这样:
[123456789]
FirstKeyword
AnotherKeyWord
[123789456]
Wow
Such
Code ...
我目前正在尝试制作一个允许成员设置特定关键字的机器人,然后机器人会在服务器中检查(在一段时间内)。如果机器人在消息中检测到该关键字(仅通过另一个 bot/webhook),它会提醒设置该关键字的用户。
基本上我想做的是这样的场景:
成员 1 --->
!setkeyword new link
(bot 然后将成员 1 的关键字具体设置为 "new link")
!listkeywords
(bot returns word/phrase "new link")
成员 2 --->
!setkeyword 新鞋
(bot 然后将成员 1 的关键字具体设置为 "new shoe")
!listkeywords
(bot returns word/phrase "new shoe")
我最擅长的是使用字典和列表。字典的关键是设置关键字的成员的用户标识,列表包含关键字。
变体 1: 字典 = {}
@bot.command()
async def add(ctx,keyword):
listy = []
listy.append(keyword)
dictt[ctx.author.id] = listy
变体 2:
dictt = {}
listy = []
@bot.command()
async def add(ctx,keyword):
listy.append(keyword)
dictt[ctx.author.id] = listy
变体 1:这导致每次用户向机器人发送消息时都会生成一个新列表。这意味着对于每个唯一用户,列表中只有一个关键字。因此,如果用户尝试添加多个关键字,则只会将最新的关键字添加到列表中
变体 2:这导致来自唯一用户的关键字被添加到同一个列表,这意味着每个用户可以添加多个关键字,但它们对每个用户来说并不是唯一的。
如何让每个唯一用户拥有自己唯一的列表,并且仍然能够向其中添加多个关键字?
有一个全局 dict
并在追加之前检查用户的 ID 是否存在,如果不存在则用新词创建一个新列表。
memory = {}
def add(id, word):
if id in memory.keys():
memory[id].append(word)
else:
memory[id] = [word]
我的建议是保存文件。设置变量将在关闭或重新启动或崩溃时被破坏...
filepath = os.path.dirname(os.path.realpath(__file__))
config = configparser.ConfigParser()
config.optionxform = str
try:
config.read(f'{filepath}/data/keywords.cfg')
user1 = config['User1'] #This might be an Idea if you have every user stored in vars
except Exception as error:
print(f" -- ERROR : File not detected.\n{error}")
quit()
#Or use it like this
@bot.command()
async def add(ctx,keyword):
keywordlist = config.items(ctx.message.user.id, raw=True)
keywordlist.append(keyword)
config[ctx.message.user.id] = dict(keywordlist)
@bot.command()
async def list(ctx):
keywordlist = config[ctx.message.user.id]
await ctx.send(keywordlist)
文件看起来像这样:
[123456789]
FirstKeyword
AnotherKeyWord[123789456]
Wow
Such
Code ...