一个不和谐的机器人命令使其他命令不起作用
Posted
技术标签:
【中文标题】一个不和谐的机器人命令使其他命令不起作用【英文标题】:one discord bot command makes other command not working 【发布时间】:2020-09-25 00:05:39 【问题描述】:import asyncio
import discord
client = discord.Client()
token = "########################"
@client.event
async def on_ready():
print("Logged in as ")
print(client.user.name)
print("===========")
@client.event
async def on_message(message):
if message.content.lower().startswith('!디엠 '):
if (str(message.author.id) != "451744624965582848"):
await message.channel.send(":no_entry_sign: 최고 관리자만 전체 DM을 요청할 수 있어요.")
return None
DM내용 = message.content.lower()[4:]
for guild in client.guilds:
members = guild.members
for member in members:
if member.bot is True:
continue
channel = await member.create_dm()
embed = discord.Embed(title='???????????? ???????????????? 전체 공지', color = FFFF33)
embed.add_field(name='???????????? ????????????????에서 발송된 DM입니다.', value=DM내용, inline=False)
embed.set_footer(text="OWS SHOP DM봇")
await channel.send(embed=embed)
await message.channel.send('메세지가 성공적으로 전달되었어요.')
await client.process_commands(message)
@client.event
async def on_member_join(member):
embed = discord.Embed(title='환영합니다', colour = discord.Colour.blue())
embed.add_field(name=f'member.name님, OWS SHOP의 모든 제품을 마음껏 둘러보세요!', value="모든 구매 문의는 <@451744624965582848>에게 주시면 감사하겠습니다.", inline=False)
embed.set_footer(text="OWS SHOP 인사봇")
await client.get_channel(717379135609110615).send(embed=embed)
@client.event
async def on_member_remove(member):
embed = discord.Embed(title='안녕히 가세요', colour = discord.Colour.red())
embed.add_field(name=f'member.name님이 나가셨어요.', value="다음에 또 와주시면 감사하겠습니다!", inline=False)
embed.set_footer(text="OWS SHOP 인사봇")
await client.get_channel(717379135609110615).send(embed=embed)
@client.event
async def on_message(message):
if (str(message.channel.id) == "717379135835603005"):
if (message.content.find("@everyone") != -1):
await message.delete()
await message.channel.send(":no_entry_sign: <@"+str(message.author.id)+">님, 중고장터에서는 everyone을 사용하실 수 없어요.")
else:
if (message.content.find("@here") != -1):
await message.delete()
await message.channel.send(":no_entry_sign: <@"+str(message.author.id)+">님, 중고장터에서는 here을 사용하실 수 없어요.")
client.run(token)
我使用 Discord.py 制作了“DM ALL USERS IN SERVER”和“欢迎,再见消息”和“检测每个人或此处并删除和警告”机器人。 “检测所有人或此处并删除并警告”功能有效,但“DM ALL USERS IN SERVER”功能无效。
@client.event
async def on_message(message):
if (str(message.channel.id) == "717379135835603005"):
if (message.content.find("@everyone") != -1):
await message.delete()
await message.channel.send(":no_entry_sign: <@"+str(message.author.id)+">님, 중고장터에서는 everyone을 사용하실 수 없어요.")
else:
if (message.content.find("@here") != -1):
await message.delete()
await message.channel.send(":no_entry_sign: <@"+str(message.author.id)+">님, 중고장터에서는 here을 사용하실 수 없어요.")
这部分效果很好,但是
async def on_message(message):
if message.content.lower().startswith('!디엠 '):
if (str(message.author.id) != "451744624965582848"):
await message.channel.send(":no_entry_sign: 최고 관리자만 전체 DM을 요청할 수 있어요.")
return None
DM내용 = message.content.lower()[4:]
for guild in client.guilds:
members = guild.members
for member in members:
if member.bot is True:
continue
channel = await member.create_dm()
embed = discord.Embed(title='???????????? ???????????????? 전체 공지', color = FFFF33)
embed.add_field(name='???????????? ????????????????에서 발송된 DM입니다.', value=DM내용, inline=False)
embed.set_footer(text="OWS SHOP DM봇")
await channel.send(embed=embed)
await message.channel.send('메세지가 성공적으로 전달되었어요.')
await client.process_commands(message)
这部分效果不好。
@client.event
async def on_message(message):
if (str(message.channel.id) == "717379135835603005"):
if (message.content.find("@everyone") != -1):
await message.delete()
await message.channel.send(":no_entry_sign: <@"+str(message.author.id)+">님, 중고장터에서는 everyone을 사용하실 수 없어요.")
else:
if (message.content.find("@here") != -1):
await message.delete()
await message.channel.send(":no_entry_sign: <@"+str(message.author.id)+">님, 중고장터에서는 here을 사용하실 수 없어요.")
如果我删除这部分,(!디엠) 效果会很好。什么是问题?
【问题讨论】:
【参考方案1】:在 on_message 事件的最后,添加 await client.process_commands(message)
【讨论】:
【参考方案2】:当您使用discord.Client()
时,您的代码中每个事件只能有一个。没有必要有多个——你可以在多个事件中做的任何事情,你也可以在一个事件中做。使用if
/elif
语句。
如果您确实包含多个事件,则最近定义/注册的事件将是“活动”事件。
我建议研究使用命令装饰器创建命令,因为这比将它们全部塞进on_message
事件中要方便得多;
@client.command(name="디엠") # the string contents is what the command will be in discord
async def mycmd(ctx): # try to avoid having non-ASCII characters as identifiers
if ctx.author.id != 451744624965582848: # IDs are ints in rewrite, no need to convert to str
await ctx.send(":no_entry_sign: 최고 관리자만 전체 DM을 요청할 수 있어요.")
else:
# rest of the command's code
@client.command()
async def othercmd(ctx): # same concept applies if you want multiple commands
# some more code here
@client.event
async def on_message(message):
await client.process_commands(message)
if message.channel.id == 717379135835603005: # same deal here with the IDs
# rest of the code
elif other_expression == "something else":
# do some other stuff
# etc.
参考资料:
Multiple events with aBot
instance
Major changes in rewrite - 查看 ID 是怎样的 int
s。
Using command decorators
Bot.process_commands()
【讨论】:
以上是关于一个不和谐的机器人命令使其他命令不起作用的主要内容,如果未能解决你的问题,请参考以下文章