Discord.py can't define channel
Question:
I’m making a discord bot that sends a welcome message to a specified channel in the .jason file. So it could be compatible with multiple servers. But it runs into an error and I can’t figure out how to fix it.
@client.command()
@commands.has_permissions(administrator=True)
async def welcomeMessage(ctx):
with open("guild.json", "r") as f:
guildInfo = json.load(f)
guildInfo[ctx.message.guild.id] = ctx.message.channel.id #sets the channel it was sent to as the default welcome message channel
with open("guild.json", "w") as f:
json.dump(guildInfo, f)
@client.event
async def on_member_join(member):
with open("guild.json", "r") as f:
guildInfo = json.load(f)
channel = guildInfo[ctx.message.guild.id] #this keeps causing the error
embed = discord.Embed(colour=0x07faf6, description=f"***Welcome to the Server***")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}")
embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await channel.send(embed=embed)
I have got everything working except this:
channel = guildInfo[ctx.message.guild.id]
It keeps giving me this error:
channel = guildInfo[ctx.message.guild.id]
AttributeError: module 'ctx' has no attribute 'message'
Answers:
The on_member_join
event does not provide a context. Luckily, the member passed to the on_member_join
event has a guild
attribute capturing the guild the member belongs to (or in this case, joined). Replace your ctx.message.guild.id
by member.guild.id
and you should be good
Thanks to Lukas Thaler’s answer, I redid the code and it works:
@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def welcomechannel(ctx):
fileopen = str(ctx.message.guild.id) + ".json"
file = open(fileopen, "w")
data = {}
data["channel"] = ctx.message.channel.id
json.dump(data, file)
print("The channel has been added to the list")
@client.event
async def on_member_join(member):
colours = [0x3348FF, 0x000000, 0xFFFFFF]
try:
fileopen = str(member.guild.id) + ".json"
file = open(fileopen, "r")
data = json.load(file)
channelid = data["channel"]
channel = client.get_channel(id=channelid)
embed = discord.Embed(colour=random.choice(colours), description="***Welcome***")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}")
embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await channel.send(embed=embed)
except:
print("The server doesn't have a welcome channel")
This answer was posted as an edit to the question Discord.py can't define channel [fixed] by the OP McBigBob under CC BY-SA 4.0.
I’m making a discord bot that sends a welcome message to a specified channel in the .jason file. So it could be compatible with multiple servers. But it runs into an error and I can’t figure out how to fix it.
@client.command()
@commands.has_permissions(administrator=True)
async def welcomeMessage(ctx):
with open("guild.json", "r") as f:
guildInfo = json.load(f)
guildInfo[ctx.message.guild.id] = ctx.message.channel.id #sets the channel it was sent to as the default welcome message channel
with open("guild.json", "w") as f:
json.dump(guildInfo, f)
@client.event
async def on_member_join(member):
with open("guild.json", "r") as f:
guildInfo = json.load(f)
channel = guildInfo[ctx.message.guild.id] #this keeps causing the error
embed = discord.Embed(colour=0x07faf6, description=f"***Welcome to the Server***")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}")
embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await channel.send(embed=embed)
I have got everything working except this:
channel = guildInfo[ctx.message.guild.id]
It keeps giving me this error:
channel = guildInfo[ctx.message.guild.id]
AttributeError: module 'ctx' has no attribute 'message'
The on_member_join
event does not provide a context. Luckily, the member passed to the on_member_join
event has a guild
attribute capturing the guild the member belongs to (or in this case, joined). Replace your ctx.message.guild.id
by member.guild.id
and you should be good
Thanks to Lukas Thaler’s answer, I redid the code and it works:
@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def welcomechannel(ctx):
fileopen = str(ctx.message.guild.id) + ".json"
file = open(fileopen, "w")
data = {}
data["channel"] = ctx.message.channel.id
json.dump(data, file)
print("The channel has been added to the list")
@client.event
async def on_member_join(member):
colours = [0x3348FF, 0x000000, 0xFFFFFF]
try:
fileopen = str(member.guild.id) + ".json"
file = open(fileopen, "r")
data = json.load(file)
channelid = data["channel"]
channel = client.get_channel(id=channelid)
embed = discord.Embed(colour=random.choice(colours), description="***Welcome***")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}")
embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
embed.timestamp = datetime.datetime.utcnow()
await channel.send(embed=embed)
except:
print("The server doesn't have a welcome channel")
This answer was posted as an edit to the question Discord.py can't define channel [fixed] by the OP McBigBob under CC BY-SA 4.0.