首页 > 解决方案 > 使用 Discord.py 在有权限的情况下发送嵌入频道会引发异常

问题描述

首先,如果我做错了什么,我很抱歉。这是我的第一个问题。我目前正在尝试使用 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() missing 1 required positional argument: '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)

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

标签: pythonexceptionpermissionsdiscord.pyembed

解决方案


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

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

推荐阅读