我想制作一个黑名单命令来阻止来自特定公会的邀请 discord.py
I want to make a blacklist command that blocks an invite from a specific guild discord.py
我对这一切都很陌生。我想制作一个命令来阻止用户从列入黑名单的公会发送 link。
该命令将如下所示:
!黑名单(公会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
我对这一切都很陌生。我想制作一个命令来阻止用户从列入黑名单的公会发送 link。 该命令将如下所示: !黑名单(公会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