简体   繁体   中英

I want my discord bot to send a message to a specific channel

I want to make a discord bot where users can set a specific channel and have my bot send a message in it. Kinda like a clash of clans global chat. Where users across servers can talk together.

Here is my code:

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)

Ive tried everything to my knowledge but im very new to all of programming. Ive tried using a webhook but found out that didnt work.

Whenever i send a message with my bot using either of the two commands i get a "sending.." message and after a few seconds i get a "The application did not respond.." message.

So just if for example you wanted it to be in #general and you typed the comamnd in #media it would send to #general?

If so you could do something like this

    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}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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