我正在写一个简单的discord bot,很有趣,我希望它从特定短语后面的消息中获取(例如)一个数字,以后再使用例如,某个用户键入“ $ test 6”,我希望我的机器人将这个数字(6)用作其他示例,然后将其发送到同一频道。
我在创建此问题以拆分message.content后出现能行吗(不算用户可以写数字以外的内容)
import discord
client = discord.Client()
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$test"):
stringOne = str(message.content)
stringTwo = "$test "
split = stringOne.partition(stringTwo)[2]
await message.channel.send("number/s from your message: " + split)
这可以通过命令参数来完成:
@bot.command()
async def test(ctx, number = None):
if not number:
await ctx.send("Please enter a number, for example: `$test 5`")
else:
await ctx.send(f"Here's the number you gave me: {number}")
以及使用不和谐对象的示例:
@bot.command()
async def kick(ctx, member: discord.Member = None, *, reason = "Default reason"):
if not member:
await ctx.send("Please provide a member for me to kick!")
else:
await member.kick(reason=reason)
await ctx.send(f"Successfully kicked {member} for `{reason}`")
参考:
*
usage in parameters*
-用于为您的机器人创建/注册命令的装饰器。commands.Command()
-这将始终作为命令中的第一个参数传递。commands.Command()
-commands.Context
继承自commands.Context
,意味着您可以向执行命令的通道发送消息。Context.send()
Context.send()
Context