[英]How do I take the end of a message on discord and make my bot repeat it?
我如何让我的漫游器在消息结尾处重复并在消息后面重复该消息? 这是我的代码:
elif message.content.startswith('/ban'):
bant = message.content.endswith('') and ('has been bant')
await client.send_message(message.channel, bant)
如果我说例如/ ban chiken
我要说的是:chiken已经弯腰
或者,如果我说/ ban jeff
我想说:杰夫一直很想
message.content
是类似于"/ban jeff"
的字符串
我们可以使用str.split
在第一个空格处分割消息
_, target = message.content.split(' ', 1)
target
将是"jeff"
对于更长的字符串/ban jeff andy
,我们只拆分一次,所以target
将是"jeff andy"
。
然后,我们可以使用它来建立我们的回应
bant = '{} has been bant'.format(target)
discord.Message.content
返回一个字符串,因此您需要执行简单的字符串操作。 幸运的是,python是一种非常不错的语言。
# There are various ways to address the issue.
# These are ranked from most recommended to least
# assuming content is: '/ban daisy boo foo'
arguments = message.content.lstrip('/ban')
# returns 'daisy boo foo'
arguments = message.content.split(' ', 1)
# returns 'daisy boo foo'
arguments = shlex.split(message.content)[1:]
# returns ['/ban', 'daisy', 'boo', 'foo']
# but we slice it to just ['daisy', 'boo', 'foo']
arguments = message.content[len('/ban'):]
# returns 'daisy boo foo'
如果您正在考虑使用此if / elif内容以某种模式方法开始为您的bot提供命令功能,那么我不推荐使用它,discord.py附带了自己的命令扩展以直接解决这些问题。
作为如何使用命令扩展名执行此操作的基本示例,下面是一些代码。
(假设discord.py重写了1.0.0a )
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='/')
@bot.command()
async def ban(ctx, user: discord.Member):
await user.ban()
@ban.error
async def ban_error(ctx, error):
await ctx.send(error)
bot.run('token')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.