Dolphin
Dolphin

Reputation: 31

I am trying to compare a variable with a set but keep getting errors

I am trying to compare a variable with a set but I keep getting an error

In the code below I am trying to see if the user's ID is in the code of pre determined IDs called "authUsers" but for some reason it isn't working

import discord
client = discord.Client()
authUser = {'usrID1','usrID2','userID3'}

@client.event
async def on_message(message):
  if message.content.lower().startswith('.test'):
    if True :{
      print('rcvd'),
      }
    if message.author.id in authUser:
      print('rcvd')
      embed1 = discord.Embed(title='Hello World!')
      await message.channel.send(embed=embed1) #this sends and embed saying "hello world" if it runs succesfully

import os
client.run(os.getenv('TOKEN'))

Upvotes: 0

Views: 38

Answers (2)

Der Pruefer
Der Pruefer

Reputation: 75

I think you could fo with a for loop too:

import discord
client = discord.Client()
authUser = {'usrID1','usrID2','userID3'}

@client.event
async def on_message(message):
  if message.content.lower().startswith('.test'):
    if True :{
      print('rcvd'),
      }
    for element in authUser:
        if message.author.id in element:
          print('rcvd')
          embed1 = discord.Embed(title='Hello World!')
          await message.channel.send(embed=embed1) #this sends and embed saying #"hello world" if it runs succesfully
        else:
            print(f"auth not succesful with user {element}.")

import os
client.run(os.getenv('TOKEN'))

Upvotes: 0

Broken DEV
Broken DEV

Reputation: 49

The problem is that user ids are ints, not strs so instead of authUser = {'usrID1','usrID2','userID3'}, you can use authUser = {1, 2, 3}

Put the ids of the users that you want in place of 1, 2, 3.

The code:

import discord
import os

client = discord.Client()
authUser = {1234, 5678, 9012}

@client.event
async def on_message(message):
  if message.content.lower().startswith('.test'):
    if True :{
      print('rcvd'),
      }
    if message.author.id in authUser:
      print('rcvd')
      embed1 = discord.Embed(title='Hello World!')
      await message.channel.send(embed=embed1) #this sends and embed saying "hello world" if it runs succesfully

client.run(os.getenv('TOKEN'))

Upvotes: 1

Related Questions