python 用户前缀,代码在www.example.com重写分支中不起作用discord.py

xjreopfe  于 2022-10-30  发布在  Python
关注(0)|答案(2)|浏览(170)

我想出来了,我只是缺少一个返回语句

def get_prefix(bot, message):
   if message.author.id == 1:
      return ["user prefix"]
   else:
      return ["default prefix"]
nom7f22z

nom7f22z1#

从你的帖子中我可以看出,你正在尝试创建一个可自定义的前缀,每个服务器都是唯一的?这不是唯一的方法,但在我的bot中,我使用一个json文件来添加和存储所有用户的前缀。这是一个非常基本的方法,但它对我的bot是这样工作的;考虑到它确实需要一点点了解json文件是如何工作的。这里有一点代码,我认为你正在寻找:

import json # put this wherever you put your other imports

def get_prefix(client, message): # have this below your on_ready event
    if not message.guild:
        return "your prefix"

    try:
        with open("./prefixes.json", "r") as f: # prefixes is just an example filename. You can use whatever you want for your json prefixes filename
            prefixes = json.load(f)

        return prefixes[str(message.guild.id)]

    except nextcord.DiscordException: # i'm using nextcord, a discord.py fork but it should be essentially the same with dpy
        return "your prefix"

上面的代码获取了json文件中标识的公会/服务器的前缀,但是您还需要一个命令来允许某些用户更改bot的前缀。

@client.command()
    @commands.has_permissions(administrator=True) # or whatever permissions you want to require the user changing the prefix to have
    async def prefix(ctx, prefix):
        with open('./prefixes.json', 'r') as f: # same file as the one from before
            prefixes = json.load(f)

        prefixes[str(ctx.guild.id)] = prefix
        try:
            with open('./prefixes.json', 'w') as f:
                json.dump(prefixes, f, indent=4) # changing the actual prefix from default to new prefix as specified in command
            embed = nextcord.Embed(description=f'Prefix successfully changed to {prefix}') # this is an example message I use just to say to the user that the prefix has been successfully changed
            await ctx.channel.send(embed=embed) # sends the embed you just created
        except nextcord.DiscordException: # general exception for discord, will change for you using discord.py but it should only be a small change
            return

另外,你需要创建或添加on_guild_remove和on_guild_join,以便在remove时,删除json文件中带有公会id和它的特定前缀的行,在join时,创建一个带有公会id和它的默认前缀的新行。

@client.event() # if in cog it would be a listener instead
    async def on_guild_join(self, guild):

        with open("./prefixes.json", "r") as f:
            prefixes = json.load(f)

        prefixes[str(guild.id)] = "your prefix"

        with open("./prefixes.json", "w") as f:
            json.dump(prefixes, f, indent=4)

# this event here would create something that looks like this inside your json file when your bot joins a new server: { "random guild id number": "your prefix" }

    @client.event() # listener if in cog
    async def on_guild_remove(guild):

        with open("./prefixes.json", "r") as f:
            prefixes = json.load(f)

        prefixes.pop(str(guild.id))

        with open("./prefixes.json", "w") as f:
            json.dump(prefixes, f, indent=4)

        # loads your json file where the prefixes are stored and removes the row which stores the no longer joined guild

如果您对概念或代码有任何问题,只需添加评论,我会尽快回复您。这段代码应该可以工作,但我写这一切有点快,所以请随时纠正我的任何错误。希望这能回答您的问题!

xbp102n0

xbp102n02#

`def get_prefix(bot, message):
  m = message.author
  if m.id == 0000000:
     return ["your prefix here"]
  else:
     return ["default prefix here"]

设置

... = commands.Bot(command_prefix=get_prefix, ...)

注意,你不需要调用函数

相关问题