Discord py 让机器人在提到时做出回应,但带有单独的公会集前缀
Discord py Getting bot to respond when mentioned but with individual guild set prefixes
所以我设置了前缀,这样每个公会都可以通过命令单独更改它。
我希望能够提及机器人并且它以“我的前缀是:”响应,但也响应使用设置前缀调用的命令。
目前如果我添加 when
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
def Mentioned_prefix(client, message):
prefix = get_prefix(client.message)
return commands.when_mentioned_or(f'{prefix}')
client = commands.Bot(command_prefix = Mentioned_prefix, intents=intents)
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '%'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.command()
@commands.has_permissions(administrator=True)
async def setprefix(ctx, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix is now: {prefix}')
@client.event
async def on_message(message):
if client.user.mentioned_in(message):
prefix = get_prefix(client, message)
await message.channel.send(f"Prefix is:' {prefix} '")
您应该创建一个单独的函数来获取公会的前缀。您可以检查公会的 ID 是否在您的 'prefixes.json' 文件中,然后 return 这个。可以在下面的代码中找到进一步的解释。
def prefix_check(guild):
# Check if this is a dm instead of a server
# Will give an error if this is not added (if guild is None)
if guild == None:
return "!"
try:
# Check if the guild id is in your 'prefixes.json'
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
p = prefixes[str(guild.id)]
except:
# Otherwise, default to a set prefix
p = "!"
# If you're confident that the guild id will always be in your json,
# feel free to remove this try-except block
return p
# on_message event
@client.event
async def on_message(message):
if client.user.mentioned_in(message):
# This is how you call the prefix_check function. It takes a guild object
await message.channel.send(f"My prefix is {prefix_check(message.guild)}")
# Don't forget to process, otherwise your commands won't work!
await client.process_commands(message)
# In a command (using ctx), do use prefix_check(ctx.guild) to get the guild's prefix
所以我设置了前缀,这样每个公会都可以通过命令单独更改它。 我希望能够提及机器人并且它以“我的前缀是:”响应,但也响应使用设置前缀调用的命令。 目前如果我添加 when
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
def Mentioned_prefix(client, message):
prefix = get_prefix(client.message)
return commands.when_mentioned_or(f'{prefix}')
client = commands.Bot(command_prefix = Mentioned_prefix, intents=intents)
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '%'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.command()
@commands.has_permissions(administrator=True)
async def setprefix(ctx, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix is now: {prefix}')
@client.event
async def on_message(message):
if client.user.mentioned_in(message):
prefix = get_prefix(client, message)
await message.channel.send(f"Prefix is:' {prefix} '")
您应该创建一个单独的函数来获取公会的前缀。您可以检查公会的 ID 是否在您的 'prefixes.json' 文件中,然后 return 这个。可以在下面的代码中找到进一步的解释。
def prefix_check(guild):
# Check if this is a dm instead of a server
# Will give an error if this is not added (if guild is None)
if guild == None:
return "!"
try:
# Check if the guild id is in your 'prefixes.json'
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
p = prefixes[str(guild.id)]
except:
# Otherwise, default to a set prefix
p = "!"
# If you're confident that the guild id will always be in your json,
# feel free to remove this try-except block
return p
# on_message event
@client.event
async def on_message(message):
if client.user.mentioned_in(message):
# This is how you call the prefix_check function. It takes a guild object
await message.channel.send(f"My prefix is {prefix_check(message.guild)}")
# Don't forget to process, otherwise your commands won't work!
await client.process_commands(message)
# In a command (using ctx), do use prefix_check(ctx.guild) to get the guild's prefix