我在本地计算机上测试了你的代码,当我给机器人写私人消息时,它对我有效,但要在我需要的频道上工作
intents.message_content = True
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
另请参阅
Warning
在文档中:
Commands
顺便提一下
你忘了char
d
在word中
random
在url中。
完整的工作代码(有一些小的更改)
import os
import discord
import requests
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
data = response.json()
item = data[0]
quote = f"{item['q']} - {item['a']}"
return quote
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.event
async def on_message(message):
#print(client.user, message.author, message.content)
if message.author == client.user:
return
if message.content.startswith('$inspire'):
quote = get_quote()
await message.channel.send(quote)
client.run(os.environ['TOKEN'])
与相同
commands
import os
import discord
from discord.ext import commands
import requests
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
data = response.json()
item = data[0]
quote = f"{item['q']} - {item['a']}"
return quote
@bot.command()
async def inspire(ctx):
#print(dir(ctx))
#print(ctx.me, ctx.author, ctx.message.content)
quote = get_quote()
await ctx.send(quote)
bot.run(os.environ['TOKEN'])