我对制作不和谐的机器人还很陌生,我正在尝试制作一个基本的测试命令,在这里您说(前缀)测试abc,而机器人也说abc。我没有收到任何错误,但是当我键入r!test abc时,什么也没有发生。我在终端上也一无所获。
这是我的代码。
import discord
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
GUILD = os.getenv('DISCORD_GUILD') # Same thing but gets the server name
client = discord.Client()
bot = commands.Bot(command_prefix='r!')
TOKEN = 'TOKENGOESHERE'
on = "I'm up and running!"
print("Booting up...")
channel = client.get_channel(703869904264101969)
@client.event # idk what this means but you have to do it
async def on_ready(): # when the bot is ready
for guild in client.guilds:
if guild.name == GUILD:
break
print(f'{client.user} has connected to Discord! They have connected to the following server: ' #client.user is just the bot name
f'{guild.name}(id: {guild.id})' # guild.name is just the server name
)
channel2 = client.get_channel(channelidishere)
await channel2.send(on)
#everything works up until I get to here, when I run the command, nothing happens, not even some output in the terminal.
@commands.command()
async def test(ctx):
await ctx.send("test")
client.run(TOKEN)
谢谢大家!
答案 0 :(得分:1)
关键问题是您没有正确定义装饰器。由于您正在使用命令,因此只需要bot = commands.Bot(command_prefix='r!')
语句。不需要client = discord.Client()
。由于它是bot = ...
,因此所有装饰器都必须以@bot
开头。另外,您不会使用client,而是会使用channel2 = bot.get_channel(channelidishere)
之类的漫游器。
删除公会循环,并用discord.utils.get代替以获得公会-guild = discord.utils.get(bot.guilds, name=GUILD)
。这需要另一次导入-import discord
尝试一下:
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD') # Same thing but gets the server name
bot = commands.Bot(command_prefix='r!')
on = "I'm up and running!"
print("Booting up...")
@bot.event # idk what this means but you have to do it
async def on_ready(): # when the bot is ready
guild = discord.utils.get(bot.guilds, name=GUILD)
print(
f'{bot.user} has connected to Discord! They have connected to the following server: ' # client.user is just the bot name
f'{guild.name}(id: {guild.id})') # guild.name is just the server name
channel2 = bot.get_channel(channelidishere)
await channel2.send(on)
@bot.command()
async def test(ctx):
await ctx.send("test")
bot.run(TOKEN)