我希望我的机器人在点击(提到)时发送特定服务器的前缀。
我搜索了它,但它没有工作(Discord.py- How to make a bot message when its pinged?)
它停止正在执行的其他命令,等等。
这是我的密码
import random
import json
from discord.ext import commands
import numpy
from urllib.request import urlopen as url
from discord.ext.commands import cooldown, BucketType
#testbot
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
bot = commands.Bot(command_prefix = get_prefix)
@bot.event
async def on_ready():
print(f'bots up')
@bot.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '.'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@bot.event
async def on_guild_remove(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@bot.command()
async def setprefix(ctx, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'prefix set to: {prefix}')
@bot.command(description='check bot ping type .ping')
async def ping(ctx):
await ctx.send(f'{round(bot.latency*1000)}ms')
@bot.event
async def on_message(message):
if bot.user.mentioned_in(message):
print("pinged")
await message.channel.send('My prefix here is {0} '.format(bot.command_prefix))
bot.run('token')给我发这条信息
< 0xb55a16a8>的函数get_prefix
发布于 2021-03-16 10:49:51
您可以使用on_message事件来检查何时提到您的机器人。
@bot.event
async def on_message(message):
if bot.user.mentioned_in(message) and message.mention_everyone is False:
prefix= get_prefix
await message.channel.send(f"My prefix is {bot.command_prefix}")
await bot.process_commands(message) # This line makes your other commands work.最后一行可能是在使用on_message事件后停止工作的命令的解决方案。
https://stackoverflow.com/questions/66653523
复制相似问题