我正在尝试创建一个向加入服务器的用户打招呼的机器人。但是要做到这一点,以便在服务器本身而不是在DM中向人们打招呼(我发现的大多数教程都教您如何做)。
这是我到目前为止提出的。
@bot.event
async def on_member_join(member):
channel = bot.get_channel("channel id")
await bot.send_message(channel,"welcome")
但是,它不起作用,而是抛出此错误。
Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:\Users\Lenovo\Documents\first bot\bot.py", line 26, in
on_member_join
await bot.send_message(channel,"welcome")
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\client.py", line 1145, in send_message
channel_id, guild_id = yield from self._resolve_destination(destination)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\client.py", line 289, in _resolve_destination
raise InvalidArgument(fmt.format(destination))
discord.errors.InvalidArgument: Destination must be Channel, PrivateChannel,
User, or Object. Received NoneType
答案 0 :(得分:0)
您没有将正确的ID传递给get_channel
,因此它返回了None
。一种快速的获取方法是调用命令
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def get_id(ctx):
await bot.say("Channel id: {}".format(ctx.message.channel.id))
bot.run("TOKEN")
您还可以修改命令,以始终在Member
加入的服务器上以特定名称发布在频道中
from discord.utils import get
@bot.event
async def on_member_join(member):
channel = get(member.server.channels, name="general")
await bot.send_message(channel,"welcome")
答案 1 :(得分:0)
帕特里克·豪(Patrick Haugh)的答案可能是您最好的选择,但请记住以下几点。
Member
对象包含行会(服务器)以及服务器包含的文本通道。
通过使用member.guild.text_channels
,即使服务器没有“常规”聊天,您也可以确保该频道将存在。
@bot.event
async def on_member_join(member):
channel = member.guild.text_channels[0]
await channel.send('Welcome!')
答案 2 :(得分:0)
尝试一下:
@bot.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name='the name of the channel')
await channel.send(f'Enjoy your stay {member.mention}!')