久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <small id='P6wne'></small><noframes id='P6wne'>

      • <bdo id='P6wne'></bdo><ul id='P6wne'></ul>
      <legend id='P6wne'><style id='P6wne'><dir id='P6wne'><q id='P6wne'></q></dir></style></legend>
    1. <i id='P6wne'><tr id='P6wne'><dt id='P6wne'><q id='P6wne'><span id='P6wne'><b id='P6wne'><form id='P6wne'><ins id='P6wne'></ins><ul id='P6wne'></ul><sub id='P6wne'></sub></form><legend id='P6wne'></legend><bdo id='P6wne'><pre id='P6wne'><center id='P6wne'></center></pre></bdo></b><th id='P6wne'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='P6wne'><tfoot id='P6wne'></tfoot><dl id='P6wne'><fieldset id='P6wne'></fieldset></dl></div>
    2. <tfoot id='P6wne'></tfoot>

        Discord.py 變量在整個代碼中不是恒定的

        Discord.py variables not constant throughout the code(Discord.py 變量在整個代碼中不是恒定的)
        • <legend id='1cN38'><style id='1cN38'><dir id='1cN38'><q id='1cN38'></q></dir></style></legend>
            <tbody id='1cN38'></tbody>
            <tfoot id='1cN38'></tfoot>
                <bdo id='1cN38'></bdo><ul id='1cN38'></ul>
              • <small id='1cN38'></small><noframes id='1cN38'>

                <i id='1cN38'><tr id='1cN38'><dt id='1cN38'><q id='1cN38'><span id='1cN38'><b id='1cN38'><form id='1cN38'><ins id='1cN38'></ins><ul id='1cN38'></ul><sub id='1cN38'></sub></form><legend id='1cN38'></legend><bdo id='1cN38'><pre id='1cN38'><center id='1cN38'></center></pre></bdo></b><th id='1cN38'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1cN38'><tfoot id='1cN38'></tfoot><dl id='1cN38'><fieldset id='1cN38'></fieldset></dl></div>
                  本文介紹了Discord.py 變量在整個代碼中不是恒定的的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我目前正在使用 discord.py 開發(fā)一個機器人,在這種情況下使用 JSON 文件來存儲它所在的每個服務器的機器人前綴.(+ 是機器人的默認前綴)使用前綴,我正在做 4 件事:在公會加入時:向 JSON 文檔添加前綴On guild remove:從 JSON 文檔中刪除此公會的前綴+更改前綴當提到機器人時,它會輸出一條消息,說明:'你好,這個服務器的前綴是+'

                  I am currently developing a bot using discord.py and in this case using a JSON file to store the bots prefixes for each server that it is in. ( + is the bots default prefix ) With prefixes, there is 4 things I am doing: On guild join: Adds a prefix to the JSON document On guild remove: Removes the prefix for this guild from the JSON document +changeprefix and when the bot is mentioned, it outputs a message stating: 'Hello, the prefix for this server is +'

                  前 3 部分工作正常.但是,當提到該機器人時,它會輸出錯誤:

                  The first 3 parts work correctly. However, when the bot is mentioned it outputs the error:

                  await message.channel.send(f'Hi my prefix for this server is **{prefix}**)
                  NameError: name 'prefix' is not defined
                  

                  我已經(jīng)做了一些研究,并就這個問題詢問了多個幫助服務器,但是我發(fā)現(xiàn) python 沒有在這種情況下有用的常量,幫助服務器上的人不確定這個問題的答案.

                  I have done some research and asked multiple help servers about this issue however what I have found is that python does not have constants which would be useful in this situation and people on help servers were unsure of the answer to this question.

                  前綴設置的所有部分的代碼是這樣的:

                  The code for all parts of the prefix setup is this:

                  @bot.event
                  async def on_guild_join(guild):
                      with open('prefix.json', 'r') as f:
                          prefixes = json.load(f)
                  
                      prefixes[str(guild.id)] = '+'
                      prefixes = prefix
                  
                      with open('prefix.json', 'w') as f:
                          json.dump(prefixes, f, indent=4)
                  
                  # REMOVES PREFIX FROM JSON FILE
                  @bot.event
                  async def on_guild_remove(guild):
                      with open('prefix.json', 'r') as f:
                          prefixes = json.load(f)
                  
                      prefixes.pop(str(guild.id))
                  
                      with open('prefix.json', 'w') as f:
                          json.dump(prefixes, f, indent=4)
                  
                  # COMMAND TO CHANGE THE PREFIX
                  @bot.command()
                  @commands.has_permissions(manage_messages=True)
                  async def changeprefix(ctx, prefix):
                      with open('prefix.json', 'r') as f:
                          prefixes = json.load(f)
                  
                      prefixes[str(ctx.guild.id)] = prefix
                      prefix = prefix
                  
                      with open('prefix.json', 'w') as f:
                          json.dump(prefixes, f, indent=4)
                  
                      await ctx.send(f'Prefix changed to **{prefix}**')
                  

                  錯誤來自的on_message是這樣的:

                  @bot.event
                  async def on_message(message):
                      if message.content.startswith("<@bot ID>"):
                          await message.channel.send(f'Hi my prefix for this server is **{prefix}**')
                      await bot.process_commands(message)
                  

                  這在代碼中比其他部分更靠后

                  and this is further down in the code than the other parts of this

                  任何幫助將不勝感激.預期的輸出是,當它標記的機器人時,它會發(fā)送一條消息,說明服務器的當前前綴.

                  Any help would be appreciated. The expected output is so when the bot it tagged, it sends a message saying the current prefix for the server.

                  更新正如下面評論中提到的,我已經(jīng)更新了我的代碼,以便它讀取 JSON 文件并檢索前綴.這是 on_messageevent

                  Update As mentioned in a comment below, I have updated my code so it reads the JSON file and retrieves the prefix. This is the updated code in the on_messageevent

                  if message.content.startswith("<bot ID>"):
                          with open('prefix.json', 'r') as f:
                              prefixes = json.load(f)
                              prefixes[str(ctx.guild.id)] = prefix
                              await message.channel.send(f'Hi my prefix for this server is **{prefix}**')
                  

                  但是,這仍然顯示

                  NameError: name 'prefix' is not defined
                  

                  我嘗試過的其他方法我也嘗試像這樣定義 prefix:

                  Other things I have tried I also tried defining prefix like this:

                  if message.content.startswith("<@850794452364951554>"):
                          with open('prefix.json', 'r') as f:
                              prefixes = json.load(f)
                              prefixes[str(ctx.guild.id)] = prefix
                              prefix = prefix
                              await message.channel.send(f'Hi my prefix for this server is **{prefix}**')
                  

                  我在 prefixes[str(ctx.guild.id)] = prefix

                  在這兩種情況下,它都會輸出以下錯誤:

                  In both those cases, it outputs this error:

                  UnboundLocalError: local variable 'prefix' referenced before assignment
                  

                  推薦答案

                  你的代碼在很多地方對我來說毫無意義,因為你在 message 事件中使用了 ctx, 這起初是不可能的.此外,在復制代碼時,prefixes = prefix 出現(xiàn)錯誤,因此值得懷疑的是為什么這對您有效.

                  Your code makes no sense to me in many places, because you use ctx in a message event, which is not possible at first. Also, when copying the code, I get an error with prefixes = prefix, so it is questionable why this supposedly works for you.

                  您的 on_guild_join 對我來說也毫無意義,因為每個人都有默認前綴.該事件可能與您的 changeprefix 命令相關,但可以做得更好.

                  Your on_guild_join also makes no sense to me, because everyone has the default prefix. The event may be relevant for your changeprefix command, but that can be done better.

                  我稍微重寫了您的 changeprefix 命令并調整了您的 on_message 事件.

                  I rewrote your changeprefix command a bit and adjusted your on_message event.

                  看看下面的代碼:

                  @bot.event
                  async def on_message(message):
                      if message.content.startswith("What is the prefix for this guild?"):
                          with open('prefix.json', 'r', encoding='utf-8') as fp:
                              custom_prefixes = json.load(fp)
                          try:
                              prefix = custom_prefixes[f"{message.guild.id}"] 
                              await message.channel.send(f'Hi my prefix for this server is **{prefix}**')
                          except KeyError:
                              return await message.channel.send("You have not set a new prefix.") # No JSON entry
                  
                      await bot.process_commands(message)
                  
                  
                  @bot.command()
                  async def setprefix(ctx, prefixes: str):
                      with open('prefix.json', 'r', encoding='utf-8') as fp:
                          custom_prefixes = json.load(fp)
                  
                      try:
                          custom_prefixes[f"{ctx.guild.id}"] = prefixes # If the guild.id already exists
                      except KeyError: # If there is no JSON entry
                          new = {ctx.guild.id: prefixes} # New prefix with command input
                          custom_prefixes.update(new) # Add guild.id and prefix
                  
                      await ctx.send(f"Prefix set to: `{prefixes}`")
                  
                      with open('prefix.json', 'w', encoding='utf-8') as fpp:
                          json.dump(custom_prefixes, fpp, indent=2)
                  

                  由于您的 on_guild_join 事件也包含錯誤,我們必須以不同的方式確定前綴.如果我們將集合 prefix 附加到默認值,我們可以實現(xiàn)這一點.

                  As your on_guild_join event also contains errors we have to determine the prefix in a different way. We can achieve that if we append the set prefix to the default one.

                  看看下面的代碼:

                  def determine_prefix(bot, msg):
                      guild = msg.guild
                      base = [DEFAULT_PREFIX]
                  
                      with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
                          custom_prefixes = json.load(fp) # Load the custom prefixes
                  
                      if guild: # If the guild exists
                          try:
                              prefix = custom_prefixes[f"{guild.id}"] # Get the prefix
                              base.append(prefix) # Append the new prefix
                          except KeyError:
                              pass
                  
                      return base
                  
                  [...](command.prefix = determine_prefix) # Where you defined your bot
                  

                  這篇關于Discord.py 變量在整個代碼中不是恒定的的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to make a discord bot that gives roles in Python?(如何制作一個在 Python 中提供角色的不和諧機器人?)
                  Discord bot isn#39;t responding to commands(Discord 機器人沒有響應命令)
                  Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“關于我嗎?Discord 機器人的功能?(不和諧.py))
                  message.channel.id Discord PY(message.channel.id Discord PY)
                  How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 機器人?)
                  discord.py - Automaticaly Change an Role Color(discord.py - 自動更改角色顏色)

                    <tfoot id='wfsRK'></tfoot>
                    <legend id='wfsRK'><style id='wfsRK'><dir id='wfsRK'><q id='wfsRK'></q></dir></style></legend>
                  1. <small id='wfsRK'></small><noframes id='wfsRK'>

                      • <i id='wfsRK'><tr id='wfsRK'><dt id='wfsRK'><q id='wfsRK'><span id='wfsRK'><b id='wfsRK'><form id='wfsRK'><ins id='wfsRK'></ins><ul id='wfsRK'></ul><sub id='wfsRK'></sub></form><legend id='wfsRK'></legend><bdo id='wfsRK'><pre id='wfsRK'><center id='wfsRK'></center></pre></bdo></b><th id='wfsRK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='wfsRK'><tfoot id='wfsRK'></tfoot><dl id='wfsRK'><fieldset id='wfsRK'></fieldset></dl></div>
                          <bdo id='wfsRK'></bdo><ul id='wfsRK'></ul>
                            <tbody id='wfsRK'></tbody>
                            主站蜘蛛池模板: 精品欧美一区二区久久久伦 | 99在线资源 | 综合自拍| av黄色在线观看 | 美女久久视频 | 中文在线一区 | 免费在线h视频 | 午夜视频网站 | 久久精品亚洲精品国产欧美kt∨ | 国产精品揄拍一区二区久久国内亚洲精 | 欧美日韩专区 | 啪啪免费 | 国产欧美一区二区三区在线看蜜臀 | 国产在线中文字幕 | 精品久久久久一区二区国产 | 欧洲一级视频 | 欧美日韩国产一区二区三区 | 欧美偷偷 | www亚洲精品| 日韩av成人 | 亚洲 成人 在线 | 久久精品一区二区三区四区 | 成人精品免费视频 | 97精品久久 | 日韩在线不卡 | 国产精品区二区三区日本 | 欧美a区| 99国产精品久久久久老师 | 美女在线视频一区二区三区 | 男女国产视频 | 国产精品欧美一区二区三区不卡 | 日韩在线观看视频一区 | 国产成人艳妇aa视频在线 | 一区二区三区四区av | 日韩精品在线观看一区二区三区 | 国产成人免费视频网站高清观看视频 | 亚洲国产成人在线 | 欧美精品综合 | 亚州精品天堂中文字幕 | 国产精品小视频在线观看 | 国产欧美视频一区二区 |