'Verify channel and role through discord.py

I want to make it so that when someone types $verify in the #verify channel they receive the "Members" role and it deletes the $verify message. It should also be able to delete any other message sent that isn't $verify. This is my code that doesn't work:

async def on_message(message):
    delmessage = client.get_channel(976724314600644649)
    if message == "$verify":
        await message.message.delete(delete_after=5)
        user = message.message.author
        role = discord.utils.get(user.guild.roles, name="Members")
        await user.add_roles(role)
        embed = discord.Embed(title="Verified", description=f"You have been verified in LXVI", color=0x0000FF)
        await user.send(embed=embed)
    else:
        await delmessage.purge(limit=1)

This code which I've commented out-

#async def verify(ctx):
#    await ctx.message.delete()
#    user = ctx.message.author
#    role = discord.utils.get(user.guild.roles, name="Members")
#    await user.add_roles(role)
#    embed = discord.Embed(title="Verified", description=f"You have been verified in LXVI",
color=0x0000FF)
#    await user.send(embed=embed) 

-works to simply verify the user when they type $verify and it deletes the message and sends them a dm, but it doesn't account for any other messages sent in the channel.



Solution 1:[1]

This deletes every message in the channel with the id of 976724314600644649. It adds the role Members before deleting if the message content is $verify.

async def on_message(message):
  if message.channel.id == 976724314600644649:
    if message.content == "$verify":
        role = discord.utils.get(message.guild.roles, name="Members")
        await message.author.add_roles(role)
        embed = discord.Embed(title="Verified", description=f"You have been verified in LXVI", color=0x0000FF)
        await message.author.send(embed=embed)
    await message.delete()

Solution 2:[2]

There are 2 areas I would look at:

  • What security context is the script running under, specifically what user is executing it? It is probably a system security context and that context doesn't have access to the files.

  • Where is the script running and is the path to the place it is running not matching the relative paths defined.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 3nws
Solution 2 AlanC