如何解决我想创建一个黑名单命令来阻止来自特定公会 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 举报,一经查实,本站将立刻删除。