Async events in other file
up vote
0
down vote
favorite
I am making a discord bot using discord.py API. After doing some coding I've realized that I should keep the code clean and keep the commands and events in separate .py files. How could I do that event or command still listen for a trigger and are in separate files? I've tried doing it with import
but it just imports classes. Example command:
@client.command(pass_context=True)
async def kick(ctx, *, member: discord.Member = None):
if ctx.message.channel.permissions_for(ctx.message.author).administrator is True:
await client.send_message(member, settings.kick_direct)
await client.kick(member)
await client.say(settings.kick_message + member.mention + settings.kick_message2)
else:
await client.say(settings.permission_error)
python asynchronous command python-3.6 discord.py
add a comment |
up vote
0
down vote
favorite
I am making a discord bot using discord.py API. After doing some coding I've realized that I should keep the code clean and keep the commands and events in separate .py files. How could I do that event or command still listen for a trigger and are in separate files? I've tried doing it with import
but it just imports classes. Example command:
@client.command(pass_context=True)
async def kick(ctx, *, member: discord.Member = None):
if ctx.message.channel.permissions_for(ctx.message.author).administrator is True:
await client.send_message(member, settings.kick_direct)
await client.kick(member)
await client.say(settings.kick_message + member.mention + settings.kick_message2)
else:
await client.say(settings.permission_error)
python asynchronous command python-3.6 discord.py
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am making a discord bot using discord.py API. After doing some coding I've realized that I should keep the code clean and keep the commands and events in separate .py files. How could I do that event or command still listen for a trigger and are in separate files? I've tried doing it with import
but it just imports classes. Example command:
@client.command(pass_context=True)
async def kick(ctx, *, member: discord.Member = None):
if ctx.message.channel.permissions_for(ctx.message.author).administrator is True:
await client.send_message(member, settings.kick_direct)
await client.kick(member)
await client.say(settings.kick_message + member.mention + settings.kick_message2)
else:
await client.say(settings.permission_error)
python asynchronous command python-3.6 discord.py
I am making a discord bot using discord.py API. After doing some coding I've realized that I should keep the code clean and keep the commands and events in separate .py files. How could I do that event or command still listen for a trigger and are in separate files? I've tried doing it with import
but it just imports classes. Example command:
@client.command(pass_context=True)
async def kick(ctx, *, member: discord.Member = None):
if ctx.message.channel.permissions_for(ctx.message.author).administrator is True:
await client.send_message(member, settings.kick_direct)
await client.kick(member)
await client.say(settings.kick_message + member.mention + settings.kick_message2)
else:
await client.say(settings.permission_error)
python asynchronous command python-3.6 discord.py
python asynchronous command python-3.6 discord.py
asked Nov 15 at 16:05
Phillip CJ
74
74
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You need to load the extension in the file where you create your discord.py
client. See below example.
bot.py
from discord.ext import commands
client = commands.Bot(command_prefix='!')
client.load_extension('cog')
@client.event
async def on_ready():
print('client ready')
@client.command()
async def ping():
await client.say('Pong')
client.run('TOKEN')
cog.py
from discord.ext import commands
class TestCog:
def __init__(self, bot):
self.bot = bot
self.counter = 0
@commands.command()
async def add(self):
self.counter += 1
await self.bot.say('Counter is now %d' % self.counter)
def setup(bot):
bot.add_cog(TestCog(bot))
It worked although it doesn't do anything. Bot doesn't respond on command.
– Phillip CJ
Nov 15 at 16:53
I have tested the code on my side and it works fine. Are you placing bothbot.py
andcog.py
in the same folder?
– Benjin
Nov 15 at 17:05
Yeah, same folder.
– Phillip CJ
Nov 15 at 17:31
Not sure what's wrong then. Are you getting any sort of output? If not, try adding some print statements in both files to see if they load
– Benjin
Nov 15 at 17:35
Bot is definitely running, the file is loaded, although it doesn't execute a command.
– Phillip CJ
Nov 15 at 17:45
|
show 12 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323416%2fasync-events-in-other-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You need to load the extension in the file where you create your discord.py
client. See below example.
bot.py
from discord.ext import commands
client = commands.Bot(command_prefix='!')
client.load_extension('cog')
@client.event
async def on_ready():
print('client ready')
@client.command()
async def ping():
await client.say('Pong')
client.run('TOKEN')
cog.py
from discord.ext import commands
class TestCog:
def __init__(self, bot):
self.bot = bot
self.counter = 0
@commands.command()
async def add(self):
self.counter += 1
await self.bot.say('Counter is now %d' % self.counter)
def setup(bot):
bot.add_cog(TestCog(bot))
It worked although it doesn't do anything. Bot doesn't respond on command.
– Phillip CJ
Nov 15 at 16:53
I have tested the code on my side and it works fine. Are you placing bothbot.py
andcog.py
in the same folder?
– Benjin
Nov 15 at 17:05
Yeah, same folder.
– Phillip CJ
Nov 15 at 17:31
Not sure what's wrong then. Are you getting any sort of output? If not, try adding some print statements in both files to see if they load
– Benjin
Nov 15 at 17:35
Bot is definitely running, the file is loaded, although it doesn't execute a command.
– Phillip CJ
Nov 15 at 17:45
|
show 12 more comments
up vote
0
down vote
accepted
You need to load the extension in the file where you create your discord.py
client. See below example.
bot.py
from discord.ext import commands
client = commands.Bot(command_prefix='!')
client.load_extension('cog')
@client.event
async def on_ready():
print('client ready')
@client.command()
async def ping():
await client.say('Pong')
client.run('TOKEN')
cog.py
from discord.ext import commands
class TestCog:
def __init__(self, bot):
self.bot = bot
self.counter = 0
@commands.command()
async def add(self):
self.counter += 1
await self.bot.say('Counter is now %d' % self.counter)
def setup(bot):
bot.add_cog(TestCog(bot))
It worked although it doesn't do anything. Bot doesn't respond on command.
– Phillip CJ
Nov 15 at 16:53
I have tested the code on my side and it works fine. Are you placing bothbot.py
andcog.py
in the same folder?
– Benjin
Nov 15 at 17:05
Yeah, same folder.
– Phillip CJ
Nov 15 at 17:31
Not sure what's wrong then. Are you getting any sort of output? If not, try adding some print statements in both files to see if they load
– Benjin
Nov 15 at 17:35
Bot is definitely running, the file is loaded, although it doesn't execute a command.
– Phillip CJ
Nov 15 at 17:45
|
show 12 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You need to load the extension in the file where you create your discord.py
client. See below example.
bot.py
from discord.ext import commands
client = commands.Bot(command_prefix='!')
client.load_extension('cog')
@client.event
async def on_ready():
print('client ready')
@client.command()
async def ping():
await client.say('Pong')
client.run('TOKEN')
cog.py
from discord.ext import commands
class TestCog:
def __init__(self, bot):
self.bot = bot
self.counter = 0
@commands.command()
async def add(self):
self.counter += 1
await self.bot.say('Counter is now %d' % self.counter)
def setup(bot):
bot.add_cog(TestCog(bot))
You need to load the extension in the file where you create your discord.py
client. See below example.
bot.py
from discord.ext import commands
client = commands.Bot(command_prefix='!')
client.load_extension('cog')
@client.event
async def on_ready():
print('client ready')
@client.command()
async def ping():
await client.say('Pong')
client.run('TOKEN')
cog.py
from discord.ext import commands
class TestCog:
def __init__(self, bot):
self.bot = bot
self.counter = 0
@commands.command()
async def add(self):
self.counter += 1
await self.bot.say('Counter is now %d' % self.counter)
def setup(bot):
bot.add_cog(TestCog(bot))
edited Nov 15 at 17:06
answered Nov 15 at 16:26
Benjin
74328
74328
It worked although it doesn't do anything. Bot doesn't respond on command.
– Phillip CJ
Nov 15 at 16:53
I have tested the code on my side and it works fine. Are you placing bothbot.py
andcog.py
in the same folder?
– Benjin
Nov 15 at 17:05
Yeah, same folder.
– Phillip CJ
Nov 15 at 17:31
Not sure what's wrong then. Are you getting any sort of output? If not, try adding some print statements in both files to see if they load
– Benjin
Nov 15 at 17:35
Bot is definitely running, the file is loaded, although it doesn't execute a command.
– Phillip CJ
Nov 15 at 17:45
|
show 12 more comments
It worked although it doesn't do anything. Bot doesn't respond on command.
– Phillip CJ
Nov 15 at 16:53
I have tested the code on my side and it works fine. Are you placing bothbot.py
andcog.py
in the same folder?
– Benjin
Nov 15 at 17:05
Yeah, same folder.
– Phillip CJ
Nov 15 at 17:31
Not sure what's wrong then. Are you getting any sort of output? If not, try adding some print statements in both files to see if they load
– Benjin
Nov 15 at 17:35
Bot is definitely running, the file is loaded, although it doesn't execute a command.
– Phillip CJ
Nov 15 at 17:45
It worked although it doesn't do anything. Bot doesn't respond on command.
– Phillip CJ
Nov 15 at 16:53
It worked although it doesn't do anything. Bot doesn't respond on command.
– Phillip CJ
Nov 15 at 16:53
I have tested the code on my side and it works fine. Are you placing both
bot.py
and cog.py
in the same folder?– Benjin
Nov 15 at 17:05
I have tested the code on my side and it works fine. Are you placing both
bot.py
and cog.py
in the same folder?– Benjin
Nov 15 at 17:05
Yeah, same folder.
– Phillip CJ
Nov 15 at 17:31
Yeah, same folder.
– Phillip CJ
Nov 15 at 17:31
Not sure what's wrong then. Are you getting any sort of output? If not, try adding some print statements in both files to see if they load
– Benjin
Nov 15 at 17:35
Not sure what's wrong then. Are you getting any sort of output? If not, try adding some print statements in both files to see if they load
– Benjin
Nov 15 at 17:35
Bot is definitely running, the file is loaded, although it doesn't execute a command.
– Phillip CJ
Nov 15 at 17:45
Bot is definitely running, the file is loaded, although it doesn't execute a command.
– Phillip CJ
Nov 15 at 17:45
|
show 12 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323416%2fasync-events-in-other-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown