-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord_bot.py
More file actions
76 lines (63 loc) · 1.86 KB
/
discord_bot.py
File metadata and controls
76 lines (63 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import logging
import os
from dotenv import load_dotenv
from discord.ext import commands
from linebot import LineBotApi
from linebot.models import TextSendMessage
from linebot.exceptions import LineBotApiError
logger = logging.getLogger(__name__)
# load env
load_dotenv('.env')
linegroup = os.getenv("LINEGROUP")
line_bot_api = LineBotApi(os.getenv("CHANNEL_TOKEN"))
discordtoken = os.getenv("DISCORD_TOKEN")
# discord bot
discordbot = commands.Bot(command_prefix = "!")
# define global env
line = True
@discordbot.event
async def on_message(message):
if message.author == discordbot.user:
logger.debug("message from self")
return
if message.content[0] == "!":
logger.debug("discord command")
await discordbot.process_commands(message)
return
if not line:
logger.debug("send to line turn off")
return
reponse = (
f"🖥️ CHANNEL: {message.channel}\n"
f"🟢 FROM: {message.author}\n"
f"💬 {message.content}"
)
try:
line_bot_api.push_message(linegroup, TextSendMessage(text=reponse))
except LineBotApiError as e:
discordbot.channel.send("Send to line error")
@discordbot.command(
name = "lineoff",
help=':For turn off message to line.'
)
async def lineoff(ctx):
logger.debug("!lineoff")
global line
if line == True:
line = False
await ctx.channel.send("Turn off successfull !!")
else:
await ctx.channel.send("Line already turn off")
@discordbot.command(
name = "lineon",
help=':For turn on message to line.'
)
async def lineon(ctx):
logger.debug("!lineon")
global line
if line == False:
line = True
await ctx.channel.send("Turn on successfull !!")
else:
await ctx.channel.send("Line already turn on")
discordbot.run(discordtoken)