我正在尝试制作一个每 40 分钟 ping(ping 角色)到一般频道的机器人。例如在 40 分钟开始时说 @ping 角色。机器人似乎不起作用,因为我的文本频道中没有任何内容。
我的代码:
import discord
from discord.ext import commands, tasks
client = commands.Bot(command_prefix="!")
client.remove_command("help")
target_channel_id = 869727547221475388
@client.group(invoke_without_command=True)
async def help(ctx):
em = discord.Embed(title = "Help", description = "Use !help <command>. for extended information on a command.",color = ctx.author.color)
em.add_field(name = "Moderation", value = "kick,ban,unban,warn,clear5")
em.add_field(name = "Activities", value = "test, message,Games_With_Friends,FlashBang,SB")
await ctx.send(embed = em)
@client.event
async def on_ready():
print("Bot is ready")
@tasks.loop(hours=0.02777778)
@commands.has_permissions(administrator = True)
async def ping_start():
message_channel = bot.get_channel(target_channel_id)
print(f"Got channel {message_channel}")
await message_channel.send("ctx.message.guild.ping_role")
@ping_start.before_loop
async def before():
await bot.wait_until_ready()
print("Finished waiting")
ping_start.start()
client.run("Token")
答案 0 :(得分:0)
首先,您已将 discord.Bot
对象分配给 client
变量,但您仍然试图在某些地方通过 bot
变量访问该对象。
然后要 ping 一个角色,您必须通过角色 ID 获取该角色。
以下代码应该可以工作。
import discord
from discord.ext import commands, tasks
import logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(message)s",
level=logging.INFO,
)
client = commands.Bot(command_prefix="!")
target_channel_id = 869727547221475388
role_id = #write role ID here
@client.event
async def on_ready():
print("Bot is ready")
@tasks.loop(minutes=40)
@commands.has_permissions(administrator = True)
async def ping_start():
message_channel = client.get_channel(target_channel_id)
role = message_channel.guild.get_role(role_id)
print(f"Got channel {message_channel}")
await message_channel.send(role.mention)
@ping_start.before_loop
async def before():
await client.wait_until_ready()
print("Finished waiting")
ping_start.start()
client.run("TOKEN")