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”,“欢迎使用,再见消息”和“检测每个人或此处并删除并警告”机器人。 “检测所有人或此处并删除并警告”功能有效,但“在服务器中使用DM ALL USERS”功能无效。
@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을 사용하실 수 없어요.")
如果我删除此部分,(!디엠)效果很好。怎么了
答案 0 :(得分:0)
使用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.
参考:
答案 1 :(得分:0)
在on_message事件的最后,添加一个等待客户端。process_commands(message)