Reputation: 55
Trying to write a Discord Bot in Python, although having trouble. Currently receiving the error "AttributeError: 'BotClient' object has no attribute 'loop'". I've looked this up before posting, and it seems to be because of not declaring an instance of the class, however I am (see last two lines of code)... Unless something else is incorrect?
Current code is as follows:
class BotClient(discord.Client):
def __init__(self, prefix, current_game):
self.pfx = prefix
self.curr_game = current_game
async def on_ready(self):
print(f'{self.user} has connected to Discord!')
await self.change_presence(status=discord.Status.idle, activity=self.curr_game)
#@self.event
async def on_message(self, message):
if message.author == self.user:
return
if message.content.startswith('%s hello' % self.pfx):
await message.channel.send('very naisu caesar-chan')
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
CHANNEL = os.getenv('CHANNEL_TOKEN')
CUR_GAME = os.getenv('GAME_ENV')
RockBot = BotClient(".rb", CUR_GAME)
RockBot.run(TOKEN)
Upvotes: 1
Views: 1362
Reputation: 150
You are modifying the __init__()
function that extends to the class discord.Client
. Apparently, you need to initialize the __init()__
inside the class discord.Client
because your new init function overwrote the discord's init function (aka the super class init function). And this should be fairly simple to fix. Just initialize it inside your new init function:
class BotClient(discord.Client):
def __init__(self, prefix, current_game, *args, **kwargs):
self.pfx = prefix
self.curr_game = current_game
super().__init__(*args, **kwargs)
async def on_ready(self):
print(f'{self.user} has connected to Discord!')
await self.change_presence(status=discord.Status.idle, activity=self.curr_game)
#@self.event
async def on_message(self, message):
if message.author == self.user:
return
if message.content.startswith('%s hello' % self.pfx):
await message.channel.send('very naisu caesar-chan')
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
CHANNEL = os.getenv('CHANNEL_TOKEN')
CUR_GAME = os.getenv('GAME_ENV')
RockBot = BotClient(".rb", CUR_GAME)
RockBot.run(TOKEN)
Upvotes: 3