<small id='L9EUG'></small><noframes id='L9EUG'>

    1. <i id='L9EUG'><tr id='L9EUG'><dt id='L9EUG'><q id='L9EUG'><span id='L9EUG'><b id='L9EUG'><form id='L9EUG'><ins id='L9EUG'></ins><ul id='L9EUG'></ul><sub id='L9EUG'></sub></form><legend id='L9EUG'></legend><bdo id='L9EUG'><pre id='L9EUG'><center id='L9EUG'></center></pre></bdo></b><th id='L9EUG'></th></span></q></dt></tr></i><div id='L9EUG'><tfoot id='L9EUG'></tfoot><dl id='L9EUG'><fieldset id='L9EUG'></fieldset></dl></div>

        <tfoot id='L9EUG'></tfoot>

        • <bdo id='L9EUG'></bdo><ul id='L9EUG'></ul>
        <legend id='L9EUG'><style id='L9EUG'><dir id='L9EUG'><q id='L9EUG'></q></dir></style></legend>

        尝试使用 Discord.py rewrite 向特定频道发送消息,但

        时间:2023-09-09
          <tfoot id='3j7Or'></tfoot><legend id='3j7Or'><style id='3j7Or'><dir id='3j7Or'><q id='3j7Or'></q></dir></style></legend>

          <small id='3j7Or'></small><noframes id='3j7Or'>

                  <tbody id='3j7Or'></tbody>
                • <bdo id='3j7Or'></bdo><ul id='3j7Or'></ul>
                  <i id='3j7Or'><tr id='3j7Or'><dt id='3j7Or'><q id='3j7Or'><span id='3j7Or'><b id='3j7Or'><form id='3j7Or'><ins id='3j7Or'></ins><ul id='3j7Or'></ul><sub id='3j7Or'></sub></form><legend id='3j7Or'></legend><bdo id='3j7Or'><pre id='3j7Or'><center id='3j7Or'></center></pre></bdo></b><th id='3j7Or'></th></span></q></dt></tr></i><div id='3j7Or'><tfoot id='3j7Or'></tfoot><dl id='3j7Or'><fieldset id='3j7Or'></fieldset></dl></div>
                  本文介绍了尝试使用 Discord.py rewrite 向特定频道发送消息,但它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我目前正在开发一个 discord 机器人,我正在尝试在用户升级后使用 Discord.py rewrite 向特定频道发送消息,但我收到此错误:

                  I'm currently working on a discord bot and I'm trying to send a message to a specific channel using Discord.py rewrite once the user levels up, and I'm getting this error:

                     await channel.message.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")
                  AttributeError: 'NoneType' object has no attribute 'message'
                  
                  

                  所有代码如下:

                  import discord
                  from discord.ext import commands
                  
                  import json
                  import asyncio
                  
                  class Levels(commands.Cog):
                      @commands.Cog.listener()
                      async def on_message(self, message):
                          if message.author == self.bot.user:
                              return
                  
                          author_id = str(message.author.id)
                          bot = commands.Bot(command_prefix='!')
                  
                          if author_id not in self.users:
                              self.users[author_id] = {}
                              self.users[author_id]['level'] = 1
                              self.users[author_id]['exp'] = 0
                  
                          self.users[author_id]['exp'] += 1
                  
                          if author_id in self.users:
                              if self.lvl_up(author_id):
                                  channel = bot.get_channel('636399538650742795')
                                  await channel.message.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")
                  
                      def __init__(self, bot):
                          self.bot = bot
                  
                          with open(r"cogsuserdata.json", 'r') as f:
                              self.users = json.load(f)
                  
                              self.bot.loop.create_task(self.save_users())
                  
                      async def save_users(self):
                          await self.bot.wait_until_ready()
                          while not self.bot.is_closed():
                              with open(r"cogsuserdata.json", 'w') as f:
                                  json.dump(self.users, f, indent=4)
                  
                              await asyncio.sleep(5)
                  
                  
                      def lvl_up(self, author_id):
                          author_id = str(author_id)
                          current_xp = self.users[author_id]['exp']
                          current_lvl = self.users[author_id]['level']
                          if current_xp >= ((3 * (current_lvl ** 2)) / .5):
                              self.users[author_id]['level'] += 1
                              return True
                          else:
                              return False
                  
                  

                  我真的不确定这里的问题是什么,但如果有人知道这个问题,如果你能告诉我如何解决这个问题,我将非常感激.

                  I'm really not sure what the issue is here but if anyone knows the problem I would be very appreciative if you could let me know how I can correct this.

                  感谢阅读我已经尝试了好几个小时.

                  Thanks for reading I've been trying to figure this out for hours.

                  仍然有问题.

                  推荐答案

                  你得到 AttributeError 因为 channel 是 None.

                  You get AttributeError because channel is None.

                  要修复它,您需要从频道 ID 中删除引号,如下所示:

                  To fix it you need to remove quotes from channel id like this:

                  channel = bot.get_channel(636399538650742795)
                  

                  此处对此进行了描述:https://discordpy.readthedocs.io/en/latest/migrating.html#snowflakes-are-int

                  我还在下一行看到另一个错误.channel 也没有 message 属性.我认为您需要像这样修复它:

                  Also i see another error on the next line. The channel has no message attribute too. I think you need to fix it like this:

                  await channel.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")
                  

                  这篇关于尝试使用 Discord.py rewrite 向特定频道发送消息,但它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:由于“错误请求"而未发送消息与不和谐.py 下一篇:查找消息的作者

                  相关文章

                  最新文章

                    <bdo id='ZzxpK'></bdo><ul id='ZzxpK'></ul>

                    <legend id='ZzxpK'><style id='ZzxpK'><dir id='ZzxpK'><q id='ZzxpK'></q></dir></style></legend>
                    <tfoot id='ZzxpK'></tfoot>

                    1. <i id='ZzxpK'><tr id='ZzxpK'><dt id='ZzxpK'><q id='ZzxpK'><span id='ZzxpK'><b id='ZzxpK'><form id='ZzxpK'><ins id='ZzxpK'></ins><ul id='ZzxpK'></ul><sub id='ZzxpK'></sub></form><legend id='ZzxpK'></legend><bdo id='ZzxpK'><pre id='ZzxpK'><center id='ZzxpK'></center></pre></bdo></b><th id='ZzxpK'></th></span></q></dt></tr></i><div id='ZzxpK'><tfoot id='ZzxpK'></tfoot><dl id='ZzxpK'><fieldset id='ZzxpK'></fieldset></dl></div>

                      <small id='ZzxpK'></small><noframes id='ZzxpK'>