简体   繁体   English

如何使用 pycord 在特定频道中发送消息?

[英]How do I send a message in a specific channel with pycord?

What I want to do我想做的事

I'm making a bot for an announcement command, and I want to make the bot send a message in a specific channel and send a message back to the user to show that the command was sent.我正在为公告命令制作机器人,并且我想让机器人在特定频道中发送消息并将消息发送回用户以显示命令已发送。 However, I get this error:但是,我收到此错误:

I tried, but I got this error我试过了,但我得到了这个错误

After a while of waiting, and getting "The application did not respond", I finally got the error of:经过一段时间的等待,并得到“应用程序没有响应”,我终于得到了错误:

Ignoring exception in on_interaction Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/bot.py", line 727, in process_application_commands command = self._application_commands[interaction.data["id"]] KeyError: '956003758620426290'忽略 on_interaction Traceback 中的异常(最近一次调用最后):文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/bot.py”,第 727 行,在 process_application_commands command = self._application_commands[interaction .data["id"]] KeyError: '956003758620426290'

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 382, in _run_event await coro(*args, **kwargs) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/bot.py", line 1028, in on_interaction await self.process_application_commands(interaction) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/bot.py", line 742, in process_application_commands await self.sync_commands(unregister_guilds=[guild_id]) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/bot.py", line 685, in sync_commands await self.http.bulk_upsert_command_permissions(self.user.id, guild_id, guild_cmd_perms) File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 357, in request raise HTTPException(response, data) discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In 0: Invalid application command id provided回溯(最后一次调用):文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”,第 382 行,在 _run_event await coro(*args, **kwargs) 文件中“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/bot.py”,第 1028 行,在 on_interaction 等待 self.process_application_commands(interaction) 文件“/opt/virtualenvs/python3/lib/python3. 8/site-packages/discord/bot.py”,第 742 行,在 process_application_commands 等待 self.sync_commands(unregister_guilds=[guild_id]) 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ bot.py”,第 685 行,在 sync_commands 中等待 self.http.bulk_upsert_command_permissions(self.user.id, guild_id, guild_cmd_perms) 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http. py",第 357 行,在请求中引发 HTTPException(response, data) discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In 0: Invalid application command id provided

Code:代码:

import discord
import os
import random
import asyncio

testing_servers = [912361242985918464]
intents = discord.Intents().all()
bot = discord.Bot(intents=intents)

@bot.event
async def on_ready():
    print('Online!')


@bot.slash_command(guild_ids=testing_servers, name="announce", description="Make server announcements!")
async def announce(ctx, title, text, channel_id,anonymous=None):
    #response embed
    print(channel_id)
    #announcement embed
    embed_announce = discord.Embed(
        colour = discord.Colour.blue(),
        title=str(title),
        description = text
    )
    await channel_id.send(embed = embed_announce)

    embed = discord.Embed(
        colour=discord.Colour.blue(),
        title = "Sent!",
        description= "Check the channel to make sure you wrote the right thing!"
    )

    await ctx.respond(embed = embed)

Past attempts:过去的尝试:

I have tried getting the channel with:我尝试通过以下方式获取频道:

bot.get_channel(channel_id)

I also tried using channel id and #channel我也尝试使用频道 ID 和#channel

However, I get this error:但是,我收到此错误:

AttributeError: 'NoneType' object has no attribute 'send' AttributeError:“NoneType”对象没有“发送”属性

确保将整数发送到get_channel()

   await bot.get_channel(int(channel_id)).send(embed=embed_announce)

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

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