python-3.x 我希望我的不和谐机器人向特定频道发送消息

vs3odd8k  于 2023-01-10  发布在  Python
关注(0)|答案(1)|浏览(158)

我想做一个不和谐的机器人,用户可以设置一个特定的频道,让我的机器人发送消息。有点像部落冲突的全球聊天。在那里用户可以跨越服务器在一起交谈。
下面是我的代码:

import os
import discord

import config
import settings
from settings import *
from utils import *

from discord_webhook import DiscordWebhook
from discord.ext import commands
from config import *

bot = interactions.Client(token=BOT_TOKEN)
webhook = DiscordWebhook(url=WEBHOOK_URL, rate_limit_retry=True, content="Online..")
response = webhook.execute()

if __name__ == '__main__':

    config.ABSOLUTE_PATH = os.path.dirname(os.path.abspath(__file__))
    config.COOKIE_PATH = config.ABSOLUTE_PATH + config.COOKIE_PATH

    if config.BOT_TOKEN == "":
        print("Error: No bot token!")
        exit

async def register(guild):

    guild_to_settings[guild] = Settings(guild)

    sett = guild_to_settings[guild]

    try:
        await guild.me.edit(nick=sett.get('default_nickname'))
    except:
        pass

@bot.event
async def on_ready():
    print(STARTUP_MESSAGE)

    for guild in bot.guilds:
        await register(guild)
        print("Active in {}".format(guild.name))
    
    print(config.STARTUP_COMPLETE_MESSAGE)
    
@bot.command(
    name="message",
    description="This description isn't seen in UI (yet?)",
    options=[
        interactions.Option(
            name="global_message",
            description="A descriptive description",
            type=interactions.OptionType.SUB_COMMAND,
            options=[
                interactions.Option(
                    name="text",
                    description="Be careful not to leak any personal information!",
                    type=interactions.OptionType.STRING,
                    required=True,
                ),
            ],
        ),
        interactions.Option(
            name="local_message",
            description="A descriptive description",
            type=interactions.OptionType.SUB_COMMAND,
            options=[
                interactions.Option(
                    name="text",
                    description="Be careful not to leak any personal information!",
                    type=interactions.OptionType.STRING,
                    required=True,
                ),
            ],
        ),
    ],
)
async def cmd(ctx: interactions.CommandContext, sub_command: str, message: str = "", text: int = None):
    if sub_command == "global_message":
        channel = bot.get_channel(CHANNEL_ID)
        await channel.send(f"{ctx.author.mention} said: {text}")

    elif sub_command == "local_message":
        await ctx.send(f"{ctx.author.mention} said: {message}")


bot.start(BOT_TOKEN)

我试过我所知道的一切,但我对所有的编程都很陌生。我试过使用webhook,但发现不起作用。
每当我使用这两个命令中的任何一个用我的机器人发送消息时,我会得到一个"正在发送.."消息,几秒钟后我会得到一个"应用程序没有响应.."消息。

6vl6ewon

6vl6ewon1#

例如,如果你想让它在#general中,你在#media中输入命令,它会发送到#general?
如果是这样的话,您可以这样做

if sub_command == "global_message":
        channel = discord.utils.get(ctx.guild.channels, id = CHANNELIDHERE)
        await channel.send(f"{ctx.author.mention} said: {text}")

    elif sub_command == "local_message":
       channel2 = discord.utils.get(ctx.guild.channels, id = CHANNELIDHERE)
       await channel2.send(f"{ctx.author.mention} said: {message}")

相关问题