我开始用 Python 编写一个不和谐的机器人。但现在我在为一些相当大的事情而挣扎: 机器人不发送消息
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!')
general_channel = client.get_channel(798136768120881165)
join_channel = client.get_channel(798194832442130444)
@client.event
async def on_member_join(ctx, member):
await ctx.join_channel.send(f"Welcome to my server! <@userid>")
@client.event
async def on_message(context):
if message.content == "What is this?":
WhatEmbed = discord.Embed(title="This is a TestBot", description="It's my first bot ever made!", color=0x00ff00)
await context.message.channel.send(embed=WhatEmbed)
@client.command(aliases=['help'])
async def help(message):
helpEmbed = discord.Embed(title="Help", description=None, colour=discord.Colour.gray())
await message.channel.send(helpEmbed=helpEmbed)
机器人正在运行并且在线
答案 0 :(得分:1)
你的代码有点乱。
on_message
不需要 Context
经理,需要 message
help
命令不接受 message
作为参数,它接受 Context
on_member_join
只接受一个参数,member
您还应该在 client.process_commands
事件的末尾添加 on_message
并启用一些意图。
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents=intents)
@client.event
async def on_ready():
await client.wait_until_ready()
print(f"Bot is online")
@client.event
async def on_member_join(member):
join_channel = client.get_channel(798194832442130444)
await join_channel.send(f"Welcome to my server! <@userid>") # if you want to mention the member use `member.mention`
@client.event
async def on_message(message):
if message.content == "What is this?":
WhatEmbed = discord.Embed(title="This is a TestBot", description="It's my first bot ever made!", color=0x00ff00)
await message.channel.send(embed=WhatEmbed)
await client.process_commands(message)
@client.command(aliases=['help'])
async def help(ctx):
helpEmbed = discord.Embed(title="Help", description=None, colour=discord.Colour.gray())
await ctx.send(helpEmbed=helpEmbed)
我启用了 intents.members
因为您有 on_member_join
事件,请记住也在 developer portal