简体   繁体   English

使用 Discord.py 在具有权限的通道中发送嵌入会引发异常

[英]Sending an Embed in channel with permission using Discord.py raises exceptions

First of all I am sorry if I'm doing something wrong.首先,如果我做错了什么,我很抱歉。 This is my first question.这是我的第一个问题。 I am currently trying to create a Discord Bot using Python.我目前正在尝试使用 Python 创建一个 Discord Bot。

Edit: Though the answer of one person helped me a lot, my question remains, because I still have the "await coro" exception and another one was thrown after I corrected the old mistake.编辑:虽然一个人的回答对我帮助很大,但我的问题仍然存在,因为我仍然有“await coro”异常,并且在我纠正了旧错误后抛出了另一个异常。 I've updated the code and the exceptions.我已经更新了代码和异常。 Thanks for your help!谢谢你的帮助!

When I'm trying to send an embed when the bot joins the server, I get two exceptions.当我在机器人加入服务器时尝试发送嵌入时,我得到了两个例外。 Since I don't have 50 servers, I replaced the on_member_join(self) with a simple function call when something is written in a channel:由于我没有 50 台服务器,因此当在通道中写入内容时,我将 on_member_join(self) 替换为简单的 function 调用:

  1. File "...\Python\Python39\lib\site-packages\discord\client.py" await coro(*args, **kwargs)文件 "...\Python\Python39\lib\site-packages\discord\client.py" 等待 coro(*args, **kwargs)
  2. TypeError: on_message() missing 1 required positional argument: 'ctx'类型错误:on_message() 缺少 1 个必需的位置参数:'ctx'
    Though I watched videos and searched on stackoverflow, I still don't understand ctx completely.虽然我在stackoverflow上看视频和搜索,但我仍然不完全了解ctx。 That's probably the reason I'm making this mistake.这可能就是我犯这个错误的原因。 If you could help me correct the code or even explain what ctx is (is it like "this" in java?), that'd be great!如果你能帮我更正代码甚至解释什么是 ctx (它像 java 中的“这个”吗?),那就太好了!

Here's the code of the two functions trying to send an embed:这是尝试发送嵌入的两个函数的代码:

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)

Thank you for helping me: Again, I'm sorry if I'm doing something wrong.谢谢你帮助我:再次,如果我做错了什么,我很抱歉。 it's my first question.这是我的第一个问题。 Please ask if you need something.请问您是否需要一些东西。 Feedback would also be very helpful.反馈也会很有帮助。

I think you simply want to compare the content of the message to the "test" string我认为您只是想将消息的内容与"test"字符串进行比较

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM