因此,我试图让我的机器人发送一个消息在一个通道中的每一个服务器,它在。我想做一个公告说这样的话:“谢谢你邀请我的机器人到您的服务器!我们现在已经达到了x数量的用户!”下面是我的代码。
import interactions
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="A descriptive description",
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="A descriptive description",
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(1062062016602308620)
await channel.send(f"{ctx.author.mention} said: {text}")
elif sub_command == "local_message":
await ctx.send(f"{ctx.author.mention} said: {message}")
@bot.command()
async def ping(ctx):
await ctx.defer()
for guild in bot.guilds:
for text_channel in guild.text_channels:
try:
await text_channel.send("Thank you for inviting my bot to your server! We have now reached x amount of users!")
break
except: pass
await ctx.followup.send("Sent")
bot.start(BOT_TOKEN)
我使用的是库交互,os,discord-webhook和discord。
希望你们能帮助我!让我知道如果我能更好地描述它:D
到目前为止,我可以看到命令/ping,但该命令将加载几秒钟,然后无法加载。
1条答案
按热度按时间myzjeezk1#
您已经使用
discord.Client.guilds
获得了您的机器人所在的公会(bot. guilders),之后你可以使用discord.Guild.text_channels
获得公会文本通道,你可以使用try-except语句循环所有通道,这样你就可以像这样传递提升(discord.Forbidden
),如果您没有发送消息的适当权限,如果没有错误,你可以用break
语句来打破循环,否则if会继续发送消息到所有公会频道。下面是一个简单的代码,如果你想用途:
对于交互,您可以使用以下代码: