I'm following a video on how to change prefixes server specific. I changed it to use channels instead but now I got the problem that the name guild
is not defined. In the video the YouTuber has got no error. What am I doing wrong?
The Code:
@client.event
async def on_guild_join(guild):
with open("welcomechanels.json", "r") as f:
welcomechannels = json.load(f)
welcomechannels[str(guild.id)] = ""
with open("welcomechannels.json", "W"):
json.dump(welcomechannels, f)
@client.command(name="setwelcomechannel")
@commands.has_permissions(administrator = True)
async def setwelcomechannel(ctx, channel: discord.Channel):
with open("welcomechannels.json", "r") as f:
welcomechannels = json.load(f)
welcomechannels[str(guild.id)] = channel.id
with open("welcomechannels.json", "w") as f:
json.dump(welcomechannels, f)
await ctx.send(f"Der Welcomechannel ist jetzt <#{channel.id}>.")
My Error:
His Code (https://youtu.be/Hh9MYiaV9U8?t=306):
UPDATE:
If someone joins the guild (server), I get this error:
Ignoring exception in on_member_join
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 78, in on_member_join
await channel.send(f'<:899414480314859530:927212502654255125> Willkommen, {member.mention}')
AttributeError: 'NoneType' object has no attribute 'send'
The function on_member_join()
looks like this:
@client.event
async def on_member_join(member):
guild = member.guild
channel = guild.get_channel(get_welcomechannel(client,member))
e = discord.Embed(title=f"Willkommen in {guild.name}", colour=0xC0EEFF, description=
f'''
{member.mention}
<a:614619624268365829:915316001401344011> Schau doch gerne mal vorbei in... <a:614619624268365829:915316001401344011>
<a:880584719916499055:927212502570369024> <#889211881661800529>
<a:880584719916499055:927212502570369024> <#889211881288499298>
<a:880584719916499055:927212502570369024> <#889211881661800533>
<a:880584719916499055:927212502570369024> <#889211881921867866>
<a:614619624268365829:915316001401344011> Habt Spaß auf {guild.name} und seid aktiv!<a:614619624268365829:915316001401344011>
''')
e.set_image(url='https://cdn.discordapp.com/attachments/748986660674601040/927204486307524608/6841391f576d4afcb2673b9dcc4d4dd1.gif')
await channel.send(f'<:899414480314859530:927212502654255125> Willkommen, {member.mention}')
await channel.send(embed=e)
The function get_welcomechannel()
looks like this:
def get_welcomechannel(client, message):
with open("./welcomechannels.json", "r") as f:
welcomechannels = json.load(f)
return welcomechannels[str(message.guild.id)]
I added the guild id and the channel id manually but I still can't change them with the command setwelcomechannel
.
Just use
ctx.guild
. Guild isnt defined in your command because well... it doesn't know whatguild
is. You can use the.guild
property of thectx
to get the guild it was in.Also, instead of
<#{channel.id}>
, you can use the.mention
property of a channel to to the same thing.