如何解决如何使discord.py自定义前缀系统?
我想为python上的Discord机器人制作自定义前缀系统。我该怎么做?
解决方法
我假设您正在谈论让每个服务器都有一个自定义前缀。如果您使用的是异步分支,我建议您这样做。在与名为prefixes.txt
的.py文件相同的目录中创建文件。之后,只需使用下面的代码,其余的事情就会完成:
import discord
bot = discord.Client()
def get_prefix(guild_id):
file = open('prefixes.txt')
for line in file.readlines():
line = line.split(',')
if(line[0] == str(guild_id)):
return line[1]
return '!'
@bot.event
async def on_message(message):
prefix = get_prefix(message.guild.id)
command = message.content.split(' ')[0].replace(prefix,'')
if(message.content.startswith(prefix)):
if(command == 'some_command_name'):
#do stuff
if(command == 'prefix'):
file = open('prefixes.txt')
newfile = ''
for line in file.readlines():
lineSplit = line.split(',')
if(lineSplit[0] == str(message.guild.id)):
newfile += str(message.guild.id) + ',' + message.content.split(' ')[1]
else:
newfile += line
file = open('prefixes.txt','w')
file.write(newfile)
await message.channel.send('The prefix for this server is now `' + message.content.split(' ')[1] + '`')
bot.run('token')
,
创建机器人实例时,可以通过传递command_prefix
参数来设置自定义前缀:
client = commands.Bot(command_prefix = "custom_prefix_here")
,如果您使用的是重写版本,则此:
client = discord.ext.commands.Bot(command_prefix = "custom_prefix_here");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。