首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.py-如何使一个机器人消息服务器前缀,当它的点击?

Discord.py-如何使一个机器人消息服务器前缀,当它的点击?
EN

Stack Overflow用户
提问于 2021-03-16 10:37:58
回答 1查看 882关注 0票数 0

我希望我的机器人在点击(提到)时发送特定服务器的前缀。

我搜索了它,但它没有工作(Discord.py- How to make a bot message when its pinged?)

它停止正在执行的其他命令,等等。

这是我的密码

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-16 10:49:51

您可以使用on_message事件来检查何时提到您的机器人。

代码语言:javascript
复制
@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事件后停止工作的命令的解决方案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66653523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档