Reputation: 44
What i mean with 'per server' is that the channel is bound to a server Here is the code i made:
def get_channel(client,message):
with open('channels.json', 'r') as f:
channels = json.load(f)
return channels[str(message.guild.id)]
@client.event
async def on_guild_join(guild):
with open('channels.json', 'r') as f:
channels = json.load(f)
channels[str(guild.id)] = ''
with open('channels.json', 'w') as f:
json.dump(channels,f)
@client.command()
async def cc(ctx, prefix):
guild = ctx.guild
with open('channels.json', 'r') as f:
channels = json.load(f)
channels[str(guild.id)] = prefix
with open('channels.json', 'w') as f:
json.dump(channels,f)
await ctx.send('done')
This is the code where i put the function in
@client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name=get_channel)
await channel.send(f'Welcome {member.mention}')
The text in the database is correct The error i got: await channel.send(f'Welcome {member.mention}') AttributeError: 'NoneType' object has no attribute 'send'
Upvotes: 0
Views: 178
Reputation: 184
def get_channel(guild):
with open('channels.json', 'r') as f:
channels = json.load(f)
return channels[str(guild.id)]
@client.event
async def on_guild_join(guild):
with open('channels.json', 'r') as f:
channels = json.load(f)
channels[str(guild.id)] = ''
with open('channels.json', 'w') as f:
json.dump(channels,f)
@client.command()
async def channel_define(ctx, channel_name):
guild = ctx.guild
with open('channels.json', 'r') as f:
channels = json.load(f)
channels[str(guild.id)] = channel_name
with open('channels.json', 'w') as f:
json.dump(channels,f)
await ctx.send('done')
@client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name=get_channel(member.guild))
await channel.send(f'Welcome {member.mention}')
This is the corrected version of the code. But I would advise you to register channel ids rather than names though.
If you want to use the get method with ids, refer to the doc : https://discordpy.readthedocs.io/en/stable/api.html#discord.utils.get
Upvotes: 1
Reputation: 184
channel = discord.utils.get(member.guild.channels, name='get_channel')
In this line you search for a channel, among the channels of the guild the member has joined, which has the name 'get_channel'. But the get function doesn't find any, so it returns None. This channel does not exist, or the cache is not loaded for this guild.
To load the cache of a guild :
await guild.chunk()
Upvotes: 0