所以我已经看到了如何制作一个限制用户使用特定单词的机器人,但我也可以做到特定用户不能使用这些单词吗?
@client.event
async def on_message(message, user):
if user.has_role("cheffe"):
if any(word in message.content for word in hate_words):
await message.delete()
await message.channel.send("""You are not allowed to speak about animes!""")
else:
await client.process_commands(message)
else:
await client.process_commands(message)
发布于 2021-08-21 07:39:24
on_message
只接受一个参数,即"Message"
,你可以找到文档的here,你可以通过message.author
获取一个成员对象
发布于 2021-08-20 17:14:05
你当前有一个变量(我假设是list,或者dict来表示性能) 'hate_words‘,它包含禁用的单词。
例如:
hate_words = ["foo", "bar"]
然后使用以下命令检查禁止使用的单词
if any(word in message.content for word in hate_words):
...
如果您想指定每个用户的禁用单词,您可以将您的数据结构更改为列表的字典,其中字典的关键是User.name
属性,例如:
hate_words_by_user = {
"user_name_1": ["foo", "bar"],
"user_name_2": ["bar", "baz"]
}
现在,您可以使用以下命令检查特定用户的子列表:
# user.name from the parameter that your on_message function has
user_hate_words = hate_words_by_user[user.name]
if any(word in message.content for word in user_hate_words):
...
如果您只是希望特定用户不能使用您的单个hate_words
列表中的单词(我没有完全理解您所指的是哪一个),您只需添加一个变量即可
users_that_may_not_use_hate_words = ["user_name_1", "user_name_2"]
然后像这样检查:
if user.name in users_that_may_not_use_hate_words:
if any(word in message.content for word in hate_words):
...
https://stackoverflow.com/questions/68869378
复制