【问题标题】:Sending an Embed in channel with permission using Discord.py raises exceptions使用 Discord.py 在有权限的情况下发送嵌入频道会引发异常
【发布时间】:2021-08-04 23:16:14
【问题描述】:

如果我做错了什么,首先我很抱歉。这是我的第一个问题。 我目前正在尝试使用 Python 创建一个 Discord Bot。

编辑:虽然一个人的回答对我帮助很大,但我的问题仍然存在,因为我仍然有“await coro”异常,并且在我纠正了旧错误后又抛出了另一个异常。我已经更新了代码和异常。感谢您的帮助!

当我在机器人加入服务器时尝试发送嵌入时,出现两个异常。由于我没有 50 台服务器,所以当在频道中写入内容时,我将 on_member_join(self) 替换为一个简单的函数调用:

  1. 文件“...\Python\Python39\lib\site-packages\discord\client.py” 等待 coro(*args, **kwargs)
  2. TypeError:on_message() 缺少 1 个必需的位置参数:'ctx'
    虽然我在stackoverflow上看了视频和搜索,但对ctx还是不是很了解。这可能就是我犯这个错误的原因。如果您能帮我更正代码,甚至解释什么是 ctx(它像 java 中的“this”吗?),那就太好了!

这是尝试发送嵌入的两个函数的代码:

import discord
from discord.utils import get
from discord.ext import commands

Bot_prefix = "<" #Later used before every command

class MyClient(discord.Client):

    async def Joining_Server(ctx, self):
        #Get channel by name:
        channel = get(ctx.guild.text_channels, name="Channel Name")

        #Get channel by ID:
        channels_Ids = get(ctx.guild.text_channels, id=discord.channel_id)

        embed = discord.Embed(title="Thanks for adding me!", description="Try")
        
        fields = [("Prefix", "My prefix is <. Just write it in front of every command!", True), 
                  ("Changing prefix", "Wanna change my prefix? Just write \"<change_prefix\" and the prefix you want, such as: \"<change_prefix !\"", True),
                  ("Commands", "For a list of the most important commands type \"<help\"", False),
                  ("Help", "For more help, type \"<help_All\" or visit:", True)]

        for channels in self.channel_Ids:
            if(commands.has_permissions(write=True)):
                channel_verified = channels.id 
        
        await ctx.channel_verified.send(embed)

    async def on_message(message, self, ctx):
        if message.author == client.user:
            return
        if message.content == "test":
            await MyClient.Joining_Server(ctx, self)

谢谢你帮助我!再说一遍:如果我做错了什么,我很抱歉,这是我的第一个问题。请问您是否需要一些东西。反馈也会很有帮助。

【问题讨论】:

  • 为什么要创建自己的客户端实例,然后在其中使用commands.Bot?为什么不将self 放在Joining_Server 命令中?那不应该是一个简单的函数而不是一个命令吗?大多数代码对我来说真的没有意义
  • 对不起,这是我的第一个真正的项目,老实说,我有时真的不知道自己在做什么。感谢您的帮助!

标签: python exception permissions discord.py embed


【解决方案1】:

我认为您只是想将消息的内容与"test" 字符串进行比较

if message.author == client.user:
    return
if message.content == "test":
    await MyClient.Joining_Server()

【讨论】:

  • 非常感谢您的帮助!
  • 我很乐意提供帮助,如果有帮助请记得接受答案(在答案的左上角打勾)
  • (希望)完成!
  • 您不接受@ExitCode1 的答案有什么原因吗?
  • 是的,我想也许人们会认为这个问题已经完全回答了,但似乎不是这样(我在某些标题中看到 [关闭])。再次感谢您的帮助!
猜你喜欢
  • 2021-01-13
  • 1970-01-01
  • 2021-11-23
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 2011-05-18
相关资源
最近更新 更多