簡體   English   中英

使用 Discord.py 在具有權限的通道中發送嵌入會引發異常

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

首先,如果我做錯了什么,我很抱歉。 這是我的第一個問題。 我目前正在嘗試使用 Python 創建一個 Discord Bot。

編輯:雖然一個人的回答對我幫助很大,但我的問題仍然存在,因為我仍然有“await coro”異常,並且在我糾正了舊錯誤后拋出了另一個異常。 我已經更新了代碼和異常。 謝謝你的幫助!

當我在機器人加入服務器時嘗試發送嵌入時,我得到了兩個例外。 由於我沒有 50 台服務器,因此當在通道中寫入內容時,我將 on_member_join(self) 替換為簡單的 function 調用:

  1. 文件 "...\Python\Python39\lib\site-packages\discord\client.py" 等待 coro(*args, **kwargs)
  2. 類型錯誤:on_message() 缺少 1 個必需的位置參數:'ctx'
    雖然我在stackoverflow上看視頻和搜索,但我仍然不完全了解ctx。 這可能就是我犯這個錯誤的原因。 如果你能幫我更正代碼甚至解釋什么是 ctx (它像 java 中的“這個”嗎?),那就太好了!

這是嘗試發送嵌入的兩個函數的代碼:

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)

謝謝你幫助我:再次,如果我做錯了什么,我很抱歉。 這是我的第一個問題。 請問您是否需要一些東西。 反饋也會很有幫助。

我認為您只是想將消息的內容與"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